google中国编程赛的俩题 NO2!请高手做!

来源:百度知道 编辑:UC知道 时间:2024/07/06 14:30:12
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.

为了方便大家编程,我翻译了一下,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