Title:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells is those horizontally or V Ertically neighboring. The same letter cell is used more than once.
For example,
Given Board =
[ ["ABCE"], ["SFCs"], ["Adee"]]
Word =
"ABCCED"
, returns
true
,
Word =
"SEE"
, returns
true
,
Word = "ABCB"
, returnsfalse
.
idea: We first analyze the problem, how to find a matching string, the first step from the array to find a match word first character position, next is in this position in four directions (up and down) recursive search, see if there is a string matching word, If the letters in these locations are equal to the next letter in Word, the search continues from these locations. Each time we start from an element, we can set an array of access flags for the elements in the board, or we can set the accessed element to a special character, which is set to "#", if the search fails, we need to restore the letter, although a single search character can not be repeated, But each time you start with a new element, the character is still available.
Attention:
1. In order to save the time cost of searching, we should judge whether the upper and lower nodes of the current node meet the condition (coordinate condition, and character match condition) before recursion, no further recursion is He Cai.
AC Code:
On if (i-1 >= 0 && board[i-1][j] = = word[index+1]) if (Exist_helper (board, Word, I-1, J, Index+1)) C11/>return true;
Error Code: Goes to the next recursion before it is judged, resulting in a timeout.
If I,j is not in the board range and does not meet the criteria, it returns false if (I < 0 | | I >= board.size () | | J < 0 | | J >= board[0].size () | | used[ I][J] | | BOARD[I][J]! = Word[index]) return false; USED[I][J] = true; bool Res; res = exist_helper (board, Word, I-1, J, index+1, used) | | exist_helper (BOARD, Word, i+1, J, index+1, used) | | exis T_helper (board, Word, I, j-1, index+1, used) | | exist_helper (BOARD, Word, I, j+1, index+1, used);
2. This problem, the letter cannot be reused, we can choose to set the flag array, or set the letter as a special character, so that can not match (if the search again to this letter), while saving space and time, and finally remember if a single search failed (from a letter), to restore the letter. I tried to set the flag array vector<vector<bool>> used (row, vector<bool> (col, false)); But it also timed out, not yet understood.
Char ctmp = board[i][j];board[i][j] = ' # ';
Match unsuccessful, need to restore the original letter board[i][j] = ctmp;
3. Since we first determine whether the letters in the four direction match word[index+1], the match will enter the next level of recursion, so the iteration termination condition is if index reaches Word.size ()-1. Indicates that word characters are matched successfully and returns true. If you select index = = Word.size () to terminate, when index = Word.size ()-1, the next call to Word[index+1] will cross the pointer.
If all characters of Word match successfully, index reaches Word.size ()-1, recursion ends, returns true if (index = = word.size ()-1) return true;
4. The design of this problem is very clever, that is, we traverse in the main function to find the position of the first letter of Word, the solution down from this position for recursive search, search success, return true. Recursive functions are only recursively searched in four directions, and the starting position is determined in the main function.
for (int i = 0, i < row; i++) {for (int j = 0; J < Col; J + +) { if (board[i][j] = = Word[0] && E Xist_helper (board, Word, I, J, 0)) return true; } } return false;
Complexity: Worst case scenario, we need to search for each vertex once, the total time complexity O (m^2 * n^2)
AC Code:
Class Solution {Public:bool exist (vector<vector<char> > &board, String Word) {if (word.size () = = 0) return true; int row = Board.size (); int col = board[0].size (); if (row = = 0 | | col = = 0) return false; for (int i = 0, i < row; i++) {for (int j = 0; J < Col; J + +) {if (board[i ][J] = = Word[0] && exist_helper (board, Word, I, J, 0)) return true; }} return false; }private:bool Exist_helper (vector<vector<char>> board, string word, int i, int j, int index) {/ /If all characters of Word match successfully, index reaches Word.size ()-1, recursion ends, returns true if (index = = word.size ()-1) return true; Char ctmp = board[i][j]; BOARD[I][J] = ' # '; On if (i-1 >= 0 && board[i-1][j] = = word[index+1]) if (Exist_helper (board, Word, i-1, J, index +1)) return true; Under if (I+1 < Board.size () && board[i+1][j] = = word[index+1]) if (Exist_helper (board, Word, i+1, J, Ind ex+1)) return true; Left if (j-1 >= 0 && board[i][j-1] = = word[index+1]) if (Exist_helper (board, Word, I, j-1, index +1)) return true; if (J+1 < Board[0].size () && board[i][j+1] = = word[index+1]) if (Exist_helper (board, Word, I, j+1, Ind ex+1)) return true; Match unsuccessful, need to restore the original letter board[i][j] = ctmp; return false; }};
[C + +] leetcode:97 Word Search (depth first search)