Topcoder question 750 points (ANSWERS)

Source: Internet
Author: User
Tags string find

Problem Statement
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. 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.
String [], string
Method signature:
Int countpaths (string [] grid, string find)
(Be sure your method is public)

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.

{"ABC ",
"Fed ",
"Ghi "}
Returns: 1
There is only one way to trace this path. Each letter is used exactly once.

{"ABC ",
"Fed ",
"Gai "}
Returns: 2
Once we get to the 'E', we can choose one of two directions ctions for the final 'A '.

{"ABC ",
"Def ",
"Ghi "}
Returns: 0
We can trace a path for "ABC", but there's no way to complete a path to the letter 'D '.

{"AA ",
"AA "}
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.

{"Ababa ",
"Ababa ",
"Ababa "}
Returns: 56448
There are a lot of ways to trace this path.

{"AAAAA ",
There are well over 1,000,000,000 paths that can be traced.

{"AB ",
"Cd "}
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.

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: 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.