塔防三国志黄忠推图:google中国编程赛的俩题 NO2!请高手做!

来源:百度文库 编辑:中科新闻网 时间:2024/05/03 10:46:22
NO2.
Problem Statement
????
You are given a vector <string> grid representing a rectangular grid of letters. You are also given a string find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the grid more than once, but you may not stay on the same cell twice in a row (see example 6 for clarification).
You are to return an int indicating the number of ways find can be found within the grid. If the result is more than 1,000,000,000, return -1.
Definition
????
Class:
WordPath
Method:
countPaths
Parameters:
vector <string>, string
Returns:
int
Method signature:
int countPaths(vector <string> grid, string find)
(be sure your method is public)
????

Constraints
-
grid will contain between 1 and 50 elements, inclusive.
-
Each element of grid will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
-
Each element of grid will contain the same number of characters.
-
find will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
Examples
0)

????
{"ABC",
"FED",
"GHI"}
"ABCDEFGHI"
Returns: 1
There is only one way to trace this path. Each letter is used exactly once.
1)

????
{"ABC",
"FED",
"GAI"}
"ABCDEA"
Returns: 2
Once we get to the 'E', we can choose one of two directions for the final 'A'.
2)

????
{"ABC",
"DEF",
"GHI"}
"ABCD"
Returns: 0
We can trace a path for "ABC", but there's no way to complete a path to the letter 'D'.
3)

????
{"AA",
"AA"}
"AAAA"
Returns: 108
We can start from any of the four locations. From each location, we can then move in any of the three possible directions for our second letter, and again for the third and fourth letter. 4 * 3 * 3 * 3 = 108.
4)

????
{"ABABA",
"BABAB",
"ABABA",
"BABAB",
"ABABA"}
"ABABABBA"
Returns: 56448
There are a lot of ways to trace this path.
5)

????
{"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA"}
"AAAAAAAAAAA"
Returns: -1
There are well over 1,000,000,000 paths that can be traced.
6)

????
{"AB",
"CD"}
"AA"
Returns: 0
Since we can't stay on the same cell, we can't trace the path at all.
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

为了方便大家编程,我翻译了一下,E文不够好,自己想了之后翻译的,但是基本意决对够正确。刚掉线了,到现在才发上来。

程序说明

给你一个变量(String)grid表示一个字母矩阵。另外加一个变量,string类型,find,代表你要在字母矩阵中找到的字母。开始的点可以是矩阵中的任一地方。路线可以是向上,向下,向左,向右,或者对角线方向上,从而到下一个字母去。可以多次重复路径,但是不能在一行内经过同一个格子两次(见例六的详细说明)[也就是不能从自己到自己]
你要返加一个整型变量,用来表示在格子内能找到所要求字母的路径的个数。如果结果大于1,000,000,000就返回-1。

定义:
一个类 WordPath
其方法有
countPaths
参数
vector <string>, string
返回值
int
使用说明
int countPaths(vector <string> grid, string find)
(确保这个方法是公共的Public)

规定(要求)
-
grid数组要有1-50个元素,包括1,50
-
数组的每个元素是一个由1-50个大写字母组成的字符串。
-
元素与元素间,字符串的长度要相等。(以构成一个矩阵)
-
find变量是一个由1-50个大写字母组成的字符串。(包括1,50)

例1)
grid =
{"ABC",
"FED",
"GHI"}

find = "ABCDEFGHI"

返回1
只有一条路径,每个字母都走了一遍。

例2)
grid =
{"ABC",
"FED",
"GAI"}

find = "ABCDEA"

答案是2
一旦我们走到了字母E,我们可以有两个方向到达字母A。

例3)
grid =
{"ABC",
"DEF",
"GHI"}

find = "ABCD"

当然是返回0
我们可以找到ABC的路径,但是,却不能一下子从C跳到D。

例4)
grid =
{"AA",
"AA"}

find = "AAAA"

路径数 108
四个方向中,我们可以从任何一个方向开始。从每个位置,我们都可以选择三个可行的方向中的一个到达第2个字母,同样的一直到第4个字母。所以 4 * 3 * 3 * 3 = 108

例5)
grid =
{"ABABA",
"BABAB",
"ABABA",
"BABAB",
"ABABA"}

find = "ABABABBA"

返回值:56448
太多了路径可以选择啊。

例6)
grid =
{"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA"}

find = "AAAAAAAAAAA"

路径数:-1
超过了1,000,000,000条路径,所以,按规定,返回-1。

例7)
grid =
{"AB",
"CD"}

find = "AA"
返回0
我们不能停在同一个格子上,所以,没有路径可以选择。

程序说明所有权和解释权归TopCoder公司。任何未经授权、没有得到TopCoder公司的签署的授权文件而使用或者更改此信息的行为,都将受到起诉。©2003, TopCoder, Inc. All rights reserved.

类似迷宫问题用回溯法解决,只是入口点可能较多,规定尝试的顺序,如顺时针:上,右上,右,右下。。。左上,然后按照string当前位置为依据判断是否为通路,无通路时回溯,遍历完string,path++ 回溯

PS. 因为需要确定回溯的方向,所以需要一个栈