GoogleProgram competition 750True questionsGroup 5
Problem Statement
Zookeeper
You are given a string [] grid representing a rectangular grid of letters. You
Are also given a string find, a word you are to find within the grid.
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
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
Ways find can be found within the grid. If the result is more
1,000,000,000, return-1. Definition
Zookeeper
Class:
Wordpath
Method:
Countpaths
Parameters:
String [], string
Returns:
Int
Method signature:
Int countpaths (string [] grid, string find)
(Be sure your method is public)
Zookeeper
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)
Zookeeper
{"ABC ",
"Fed ",
"Ghi "}
"Abcdefghi"
Returns: 1
There is only one way to trace this path. Each letter is used exactly once.
1)
Zookeeper
{"ABC ",
"Fed ",
"Gai "}
"Abcdea"
Returns: 2
Once we get to the 'E', we can choose one of two directions ctions for the final 'A '.
2)
Zookeeper
{"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)
Zookeeper
{"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 ctions for our second letter, and again
For the third and fourth letter. 4x3*3*3 = 108. 4)
Zookeeper
{"Ababa ",
"BABAB ",
"Ababa ",
"BABAB ",
"Ababa "}
"Abababba"
Returns: 56448
There are a lot of ways to trace this path.
5)
Zookeeper
{"AAAAA ",
"AAAAA ",
"AAAAA ",
"AAAAA ",
"AAAAA "}
"Aaaaaaaaaaa"
Returns:-1
There are well over 1,000,000,000 paths that can be traced.
6)
Zookeeper
{"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.