The original title ( Medium ):
Given a two-dimensional grid and a word, to find out whether the word exists in the grid.
Words must, by the letters in adjacent cells constituting alphabetically, where "adjacent" cells that are adjacent horizontally or vertically adjacent cells. Letters within the same cell is not allowed to be reused.
Ideas: back
Establishing a xy coordinate system, implemented in the grid frame by moving vertically and laterally by four coordinates, and determines whether the mobile is natural conditions: for out of bounds (greater than or equal to 0 and less than the grid size). Moreover, it needs a two-dimensional array and grid access to standard, records are available in the grid.
And backtracking idea is: In addition to the main function, need a recursive function (named DFS), recursive function by moving in the direction of four known coordinates, if they meet the conditions of movement of the recursive call DFS to move within the grid, If the current character and the character string is equal to the current index, the set has been accessed and the grid index is incremented, and then continues to move up the recursive return ...... condition index is equal to the length of the string (T return), or current character string in the current index not wait character (return F.), or equivalent, but all characters adjacent to the character string is not equal to the next index (return F).
. 1 class Solution { 2 public : . 3 int the dir [ . 4 ] [ . 4 ] = {{- . 1 , 0 }, { . 1 , 0 }, { 0 , . 1 }, { 0 , - . 1 }}; // . 4 directions coordinates, representing left, right, up and down movement of a grid . 4 BOOL the DFS (Vector <Vector < char >> Board &, int index, int X, int Y, Vector <Vector < BOOL >> & visited, String & Word) 5 { 6 // after moving the current character string in the current index grid character comparison . 7 IF (Board [X] [Y] == Word [index] && index < word.length ()) . 8 { . 9 // equal, the index increments, which has access to the grid 10 index ++ ; . 11 visited [X] [Y] = to true ; 12 is } 13 is the else 14 { // otherwise, it returns false, a fall back on the DFS 15 return to false ; 16 } . 17 // index equal to the length of the string, indicating that the word is present in the grid, the end of the recursion, returns to true 18 is IF (word.length index == ())return to true ; . 19 20 is // . 4 mobile directions attempt 21 is for ( int I = 0 ; I < . 4 ; I ++ ) 22 is { 23 is X + = the dir [I] [ 0 ]; 24 Y + = the dir [I] [ 1 ]; 25 26 is // If this direction is not out of range, it proceeds to a grid, and recursively calls the DFS 27 IF (X> = 0 && X <board.size () && Y> = 0 && Y <Board [ 0 ] .size () &&! visited [X] [Y]) 28 IF(The DFS (Board, index, X, Y, visited, Word)) // if T, indicating the end of the recursion, continues back up T 29 return to true ; 30 31 is // if this be described the character direction is not out of range or match, return to the original position. 32 X - = the dir [I] [ 0 ]; 33 is Y - = the dir [I] [ . 1 ]; 34 is } 35 // if this be described character grid near the current character are a character string in the current index do not match, indicating that being the case, the grid has been withdrawn record access, and continue up returns F. 36 visited [X] [Y] = to false ; 37 [ return to false ; 38 is } 39 40 // main function 41 is BOOLexist (Vector <Vector < char >> & Board, String Word) { 42 is int Row = board.size (); 43 is int COL = Board [ 0 ] .size (); 44 is Vector <Vector < BOOL >> visited (Row , Vector < BOOL > (COL, to false )); // dimensional array access 45 46 is // traverse grids, to find a proper start position, i.e. in the grid and equal to the first character format that 47 for ( int I = 0 ; I <Row; I ++ ) 48 for ( int J = 0 ; J <COL; J ++) 49 if (DFS(board, 0, i, j, visited, word)) 50 return true; 51 return false; 52 } 53 };