Google Programming Contest 750 points

Source: Internet
Author: User
Tags string find

Google programming contest finals 750 true questions Fifth Group

Ideas:

Extensive search.

Data Structure: two vectors, one storing the current location current, and the other storing the next location next

Algorithm:
1. Find the first letter of the string in the matrix and put it in current.
2. Search for the character neighbors in current one by one to see if the second character in the string is placed in next.
3. Check whether the length of next exceeds 1000000.
4. Assign next to current and clear next
5. Repeat the first step

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.

 

My ideas are as follows, but the operation fails to return the correct results for reference only:
Using system;
Using system. collections;
Public class wordpath
{
Public int countpaths (string [] grid, string find)
{
Int m = 0;
Int H = grid. length;

For (INT I = 0; I {
For (Int J = 0; j <grid [I]. length; j ++)
{
If (grid [I] [J] = find [0])
{
// All
M + = getnext (grid, find, I, j, 1, M );
}
}

}
Return m;
}

Private int getnext (string [] grid, string find, int M, int N, int F, int T)
{
Int x = grid. length;
Int [,] Im = {}, {0,-1}, {}, {-}, {-}, {1,-1 }, {-1,-1 },{ 1, 1 }};

For (INT I = 0; I <8; I ++)
{
Int P = Im [I, 0] + m;
Int q = Im [I, 1] + N;
If (P>-1 & q>-1 & P <X & q <X & grid [p] [Q] = find [f])
{
If (F + 1 = find. length)
{
T + = 1;
}
Else
Getnext (grid, find, p, q, F + 1, t );
}
}

Return T;
}
}
Public class wordpath {
Private string find = "";
Private Static int COUNT = 0;
Public wordpath (){
}

Public int countpaths (string [] grid, string find ){
This. Find = find;
Char [] [] cs = new char [grid. Length] [grid [0]. Length ()];
For (INT I = 0; I <grid. length; I ++ ){
For (Int J = 0; j <grid [I]. Length (); j ++ ){
CS [I] [J] = grid [I]. charat (j );
}
}

For (INT I = 0; I <CS. length; I ++ ){
For (Int J = 0; j <CS [I]. length; j ++ ){
If (CS [I] [J] = find. charat (0 ))
{
Go (CS, 0, I, j );
}
}
}

If (count & gt; 1000000000)
Count =-1;

Return count;
}
Public void go (char [] [] CS, int index, int X, int y)
{
If (Index = find. Length ()-1)
{
Count ++;
Return;
}
For (INT I =-1; I <2; I ++ ){
For (Int J =-1; j <2; j ++ ){
If (I = 0 & J = 0)
Continue;
If (x + I <0 | x + I> CS. Length-1)
Continue;
If (Y + j <0 | Y + j> CS [0]. Length-1)
Continue;
If (CS [x + I] [Y + J] = find. charat (index + 1 ))
Go (CS, index + 1, x + I, Y + J );

}
}

}

Public static void main (string [] ARGs ){
Wordpath wordpath1 = new wordpath ();

}

}

Namespace S3 {

Const unsigned int max_count = 1000000000;
Const unsigned int max_uint = 0 xffffffff;

Struct Cell
{
Char ch;
Int count [2];
};

Class cgrid
{
Typedef cell * lpcell;

Public:
Cgrid (): m_rowcount (0), m_colcount (0), m_ppcells (null ){}
~ Cgrid () {clear ();}

Bool initialize (const STD: vector <STD: String> & grid)
{
Clear ();
M_rowcount = grid. Size ();
If (m_rowcount <= 0)
{
Return false;
}

M_colcount = grid [0]. Size ();
If (m_colcount <= 0)
{
Return false;
}

For (INT I = 0; I <m_rowcount; I ++)
{
If (grid [I]. Size ()! = M_colcount)
{
Return false;
}
}

Int ncount = m_rowcount * m_colcount;
M_ppcells = new lpcell [ncount];
If (m_ppcells = NULL)
{
Return false;
}

For (I = 0; I <m_rowcount; I ++)
{
For (Int J = 0; j <m_colcount; j ++)
{
Lpcell P = new cell;
M_ppcells [I * m_colcount + J] = P;
P-> CH = grid [I] [J];
}
}

Return true;
}

Int countpath (const STD: string & find)
{
Int nlen = find. Size ();
If (nlen <= 0)
{
Return 0;
}

Con

# --------------- In Python -----------------------

### Example 0
### Returns: 1
# Grid = ['abc', 'fed ', 'ghi']
# Find = 'abcdefghi'

### Example 1
### Returns: 2
# Grid = ['abc', 'fed ', 'gai']
# Find = 'abcdea'

### Example 2
### Returns: 0
# Grid = ['abc', 'def ', 'ghi']
# Find = 'abc'

### Example 3
### Returns: 108
# Grid = ['A', 'a']
# Find = 'aaa'

### Example 4
### Returns: 56448
# Grid = ['ababa ', 'babab', 'ababa', 'bababa', 'ababa ']
# Find = 'ababba'

### Example 5
### Returns:-1
Grid = ['aaaaa', 'aaaaa', 'aaaaaaa']
Find = 'aaaaaaaaaaa'

### Example 6
### Returns: 0
# Grid = ['AB', 'cd']
# Find = 'A'

Class wordpath:
Def _ init _ (Self ):
Pass
Def countpaths (self, grid, find ):

# Prepare the searchresult Matrix
Self. searchresult = []
For I in range (LEN (GRID )):
Self. searchresult. append ([])
For I in range (LEN (GRID )):
For J in range (LEN (grid [0]):
Self. searchresult [I]. append ({})

Count = 0
For I in range (LEN (GRID )):
For J in range (LEN (grid [0]):
Result = self. Search (grid, I, j, find)
If result =-1:
Return-1
Count = count + Result
I think this is the best answer:
# Include <iostream>
# Include <vector>
# Include <string>
# Include <algorithm>
# Include <numeric>
# Include <cmath>
# Include <time. h>
Using namespace STD;

Const int neigh [8] [2] = {}, {1,-1}, {0,-1 }, {-1,-1 }};
Const int max = 1000000000;
Class wordpath
{
PRIVATE:
Int result;
Int map [3] [52] [52];
Int X, Y;
Bool level;
Public:
Inline int fresult ()
{
Int I, J;
Result = 0;
For (I = 1; I <= x; I ++)
For (j = 1; j <= y; j ++)
{
Result + = map [level] [I] [J];
If (result> MAX) Return-1;
}
Return result;
}
Int countpaths (vector <string> grid, string findpath)
{
Int I, J, K, L, M, N, Q;
X = grid [0]. Size ();
Y = X;
Memset (MAP, 0, sizeof map );
Level = 0;
For (I = 1; I <= x; I ++)
For (j = 1; j <= y; j ++)
{
Map [2] [I] [J] = grid [I-1] [J-1];
If (Map [2] [I] [J] = findpath [0])
Map [level] [I] [J] = 1;
};
If (findpath. Length () = 1)
Return fresult ();

For (k = 1; k <findpath. Length (); k ++)
{
Level =! Level;
For (I = 1; I <= x; I ++)
For (j = 1; j <= y; j ++)
If (Map [2] [I] [J] = findpath [k])
For (L = 0; L <8; l ++)
{
Map [level] [I] [J] + = map [! Level] [I + neigh [l] [0] [J + neigh [l] [1];
If (MA algorithm is acceptable, far simpler than thinking.

//--------------------------------------
Import java. util .*;
Import java. Text .*;

Class data {
Private Static char [] [] rectangle = {
{'A', 'B', 'A', 'B', 'B', 'A '},
{'B', 'A', 'B', 'A', 'A', 'B '},
{'A', 'B', 'A', 'B', 'B', 'A '},
{'B', 'A', 'B', 'A', 'A', 'B '},
{'A', 'B', 'A', 'B', 'B', 'A '},
};
Private Static string STF = "abababba ";
Public static string stringtofind (){
Return STF;
}
Public static char [] [] rectangletosearch (){
Return rectangle;
}
Public int maximum = 100000;
}

Class position {
Private int X, Y;
Private Static int recwidth, recheight;
Private Static Data D;

Public static void Init (Data D _){
D = D _;
Recwidth = D. rectangletosearch (). length;
Recheight = D. rectangletosearch () [0]. length;
}

Public position (int x, int y ){
This. x = X;
This. Y = y;
}

Public int X (){
Return X;
}
Public int y (){
Return y;
}
Public char charvalue (){
Return D. rectangletosearch () [x] [Y];
}
Public arraylist neighbor (){
Arraylist result = new arraylist ();
Int NX, NY;
For (INT I =-1; I! = 2

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.