A solution to a Google competition problem

Source: Internet
Author: User
Tags string find

I took part in the competition of Google China Programming challenge in the early hours of December 13, 2005. Although I finally felt that I had made this game of high to mid, I found that the finalists were already over ...

I believe that many friends in Vckbase certainly also participated in the competition, so I intend to write their own solution, a although the test case is all passed, but this does not guarantee that my solution is correct, I hope everyone criticized the advice Second, I believe that other friends must have a better solution to everyone to discuss the discussion. Hope that this article can play a good effect.

First, the competition question

Problem Statement
You are given a string[] grid representing a rectangular grid of letters. are also given a String find,
A word you are to find within the grid. The starting point may is 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/may isn't stay on the same cell twice in a row (the "I example 6 for clarification").
You are are to return a int indicating the number of ways find can is 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 the IS public)
Constraints
-
Grid would contain between 1 and elements, inclusive.
-
Each element of grid would contain between 1 and uppercase (' A '-'-' Z ') letters, inclusive.
-
Each element of grid would contain the same number of characters.
-
Find would contain between 1 and 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 the three
Possible directions for we 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 the 1,000,000,000 paths can be traced.
6)
{"AB",
"CD"}
"AA"
returns:0
Since we can ' t stay on the same cell, we can ' t trace the ' path ' in all.
This problem statement are 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.

The meaning of the topic is roughly: to write a prototype in class Wordpath:int countPaths(vector < string> grid, string find)

function, grid is equivalent to an alphabetic matrix, each string in the grid contains the same number of letters, these letters are uppercase letters, from ' A ' to ' Z ', the number of letters in the grid is 1-50. Parameter find is a string that requires you to search the path in the grid, and it also contains only the characters ' a ' to ' Z ', with a range of 1-50. The beginning of the search can start at any point in the grid, and then move up, down, to the left, to the right, and diagonally to a grid. The letters for each position in the grid can be used more than once. However, the path cannot be stopped two times in the same position (see Use Case 6). The return value is an integer data representing the total number of paths searched. If this number is greater than 1,000,000,000, return-1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.