Google Programming Competition

Source: Internet
Author: User
Tags string find

The question of the Google Programming Competition is indeed a test of algorithm and thinking capabilities, and it is indeed a little difficult to complete within an hour. After testing, I found that I do not have enough in this aspect. I completed this question for nearly half a day and ran through all the cases given by the question.
Original question:
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.
Definition
Class:
Wordpath
Method:
Countpaths
Parameters:
String [], string
Returns:
Int
Method signature:
Int countpaths (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 ctions 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.

In this case, you can find the number of paths of a specified string description in the character matrix of a given string array. In the matrix, all eight directions can be moved, a character in a path can appear multiple times, but cannot be repeated multiple times at the same position. If the question is not clearly understood, let's look at the cases provided by the question.
Implementation idea: I guess this is the most stupid way. First, find the coordinates of the first character of all paths in the matrix, and then start searching for the path at each position of the first character in a loop. The search uses a recursive method of findppath, search for the next character in the path in the top, bottom, left, right, and other 8 directions. Find the character in any direction and call the recursive method. When the last character of the path is found, a path is counted. I guess it is not clearly described. Let's look at the Code:

Import java. util. arraylist;
/**
* Author: zming
* Http://blog.jweb.cn
*/
Public class wordpath {
// Path count
Private int COUNT = 0;

//
Char [] [] grid2;

/**
* @ Param Grid
* Matrix data
* @ Param find
* Path string
* @ Return: number of paths found
*/
Public int countpaths (string [] grid, string find ){
Char first;
Arraylist firstpos;
Grid2 = genarr (GRID );
Char [] findstr = find. tochararray ();
First = findstr [0];

Int [] currpos = NULL;
Firstpos = findinitposition (first );

If (firstpos. Size ()! = 0 ){
For (INT I = 0; I <firstpos. Size (); I ++ ){
Currpos = (INT []) firstpos. Get (I );
Findppath (findstr, 0, currpos );
}
}
'If (count> 1000000000 ){
Return-1;
} Else {
Return count;
}

}

/**
*
* @ Param findstr
* Path string to be searched
* @ Param POS
* Find the position of the current character in the string
* @ Param currpos
* The coordinate int [0] of the current prefix to be searched in the matrix is X, and the int [1] is Y.
*
*/
Private void findppath (char [] findstr, int POs, int [] currpos ){
Char nextchar;
Int xlen, ylen, currx, curry;

'If (count> 1000000000 ){
Return;
}

Nextchar = findstr [POS + 1];

Int [] nextpos;

Ylen = grid2.length;
Xlen = grid2 [0]. length;
Currx = currpos [0];
Curry = currpos [1];

/**
* In the matrix, check whether the next character in the path string exists in eight directions starting from the character to be searched,
* If any direction is found, the search continues recursively.
*/
// Up
If (currx-1> = 0 ){
If (grid2 [currx-1] [curry] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx-1;
Nextpos [1] = curry;
Findppath (findstr, POS + 1, nextpos );
}

}
}

If (curry + 1 <xlen & currx-1> = 0 ){
If (grid2 [currx-1] [curry + 1] = nextchar ){

If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx-1;
Nextpos [1] = curry + 1;
Findppath (findstr, POS + 1, nextpos );
}
}
}

// Right
If (curry + 1 <xlen ){
If (grid2 [currx] [curry + 1] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx;
Nextpos [1] = curry + 1;
Findppath (findstr, POS + 1, nextpos );
}
}
}

If (currx + 1 <xlen & curry + 1 <ylen ){
If (grid2 [currx + 1] [curry + 1] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx + 1;
Nextpos [1] = curry + 1;
Findppath (findstr, POS + 1, nextpos );
}
}
}

// Down
If (currx + 1 <ylen ){
If (grid2 [currx + 1] [curry] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx + 1;
Nextpos [1] = curry;
Findppath (findstr, POS + 1, nextpos );
}
}
}

If (curry-1> = 0 & currx + 1 <ylen ){
If (grid2 [currx + 1] [curry-1] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = curry-1;
Nextpos [1] = currx + 1;
Findppath (findstr, POS + 1, nextpos );
}
}
}

// Left
If (curry-1> = 0 ){
If (grid2 [currx] [curry-1] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx;
Nextpos [1] = curry-1;
Findppath (findstr, POS + 1, nextpos );
}
}
}

If (currx-1> = 0 & curry-1> = 0 ){
If (grid2 [currx-1] [curry-1] = nextchar ){
If (Pos + 1 = findstr. Length-1 ){
Count ++;

} Else {
Nextpos = new int [2];
Nextpos [0] = currx-1;
Nextpos [1] = curry-1;
Findppath (findstr, POS + 1, nextpos );
}
}
}
}

/**
* Find the positions of the first character of all paths in the matrix and return them in list form.
*
* @ Param ch
* The first character of the path
* @ Return all arrays in the matrix that meet the conditions
*/
Private arraylist findinitposition (char ch ){
Int [] chpos = NULL;
Arraylist firstpos = new arraylist ();
For (INT I = 0; I <grid2.length; I ++ ){
For (Int J = 0; j <grid2 [I]. length; j ++ ){
If (CH = grid2 [I] [J]) {
Chpos = new int [2];
Chpos [0] = I;
Chpos [1] = J;
Firstpos. Add (chpos );
}
}
}
Return firstpos;
}

/**
* Convert the input one-dimensional string array data to a two-dimensional char matrix.
*
* @ Param inarr
* @ Return
*/
Private char [] [] genarr (string [] inarr ){
Char [] [] grid = NULL;
Int x = 0, y = 0;
If (inarr. length! = 0 ){
X = inarr. length;
Y = inarr [0]. Length ();
}
Grid = new char [x] [Y];
For (INT I = 0; I <inarr. length; I ++ ){
Grid [I] = inarr [I]. tochararray ();
// For (Int J = 0; j <inarr [I]. Length (); j ++ ){
// Grid [I] [J] = inarr [I]. substring (J, J + 1 );
//}
}
Return grid;
}

Public static void main (string [] ARGs ){
Wordpath Wp = new wordpath ();
// String [] inarr = {"ABC", "Fed", "Ghi "};
// String find = "abcdefghi ";

// String [] inarr = {"ABC ",
// "Fed ",
// "Gai "};
// String find = "abcdea ";

// String [] inarr = {"AA ",
// "AA "};
// String find = "aaaa ";

// String [] inarr = {"AB "};
// String find = "ABA ";
//
String [] inarr = {"Ababa", "BABAB", "Ababa", "BABAB", "Ababa "};
String find = "abababba ";

// String [] inarr = {"AAAAA", "AAAAA "};
// String find = "aaaaaaaaaaa ";

System. Out. println ("path count:" + WP. countpaths (inarr, find ));
}
}

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.