adding the word search code
This commit is contained in:
@@ -116,15 +116,17 @@ daldruetryrt
|
|||||||
```
|
```
|
||||||
- If you can start from any location of this grid, and go forward, backward, up and down. Can you find the word **computer** in this grid? (**Note**: The same letter cell may not be used more than once.)
|
- If you can start from any location of this grid, and go forward, backward, up and down. Can you find the word **computer** in this grid? (**Note**: The same letter cell may not be used more than once.)
|
||||||
- A sketch of the solution is as follows:
|
- A sketch of the solution is as follows:
|
||||||
– The grid of letters is represented as vector<vector<char>> grid; Each vector<char> represents a row. We can treat this as a two-dimensional array.
|
– The grid of letters is represented as vector<vector<char>> grid; Each vector<char> represents a row. We can treat this as a two-dimensional array.
|
||||||
– A word to be sought, such as “computer” is read as a string.
|
– A word to be sought, such as “computer” is read as a string.
|
||||||
– A pair of nested for loops searches the grid for occurrences of the first letter in the string. Call such a location (r, c).
|
– A pair of nested for loops searches the grid for occurrences of the first letter in the string. Call such a location (r, c).
|
||||||
– At each such location, the occurrences of the second letter are sought in the 4 locations surrounding (r, c).
|
- At each location where the first letter is found, a search of the second letter is initiated in the 4 neighboring locations of location (r, c).
|
||||||
- At each location where the second letter is found, a search is initiated in the 4 locations surrouding this second letter.
|
- Make this process recursive: at each location where the *ith* letter is found, a search of the *(i+1)th* letter is initiated in the 4 neighboring locations.
|
||||||
|
- The search can stop when all letters of the string are found - this is the base case of the recursion.
|
||||||
|
- Question: how to make sure we do not use the same letter more than once on our success path?
|
||||||
|
|
||||||
## 12.8 Exercise: Complete the implementation
|
## 12.8 Exercise: Complete the implementation
|
||||||
|
|
||||||
- [Leetcode problem 79: Word Search](https://leetcode.com/problems/word-search/). Solution: To be added.
|
- [Leetcode problem 79: Word Search](https://leetcode.com/problems/word-search/). Solution: [p79_wordsearch.cpp](p79_wordsearch.cpp).
|
||||||
- [Leetcode problem 212: Word Search II](https://leetcode.com/problems/word-search-ii/). Solution: To be added.
|
- [Leetcode problem 212: Word Search II](https://leetcode.com/problems/word-search-ii/). Solution: To be added.
|
||||||
|
|
||||||
## 12.9 Summary of Nonlinear Word Search Recursion
|
## 12.9 Summary of Nonlinear Word Search Recursion
|
||||||
|
|||||||
58
leetcode/p79_wordsearch.cpp
Normal file
58
leetcode/p79_wordsearch.cpp
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
// search word[index] at locations (i,j)
|
||||||
|
bool search(vector<vector<char>>& board, string word, int i, int j, int index){
|
||||||
|
// base case
|
||||||
|
if(index>=word.length()){
|
||||||
|
// when index is equal to word length, it means our search job is done.
|
||||||
|
// and in this case, we don't care if i or j is out of bounds of not.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// boundary checking
|
||||||
|
if(i<0 || i>=board.size()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(j<0 || j>=board[0].size()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// if not equal, then this is not the path we are looking for.
|
||||||
|
if(board[i][j] != word[index]){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
char c = board[i][j];
|
||||||
|
// if it's found, we change it to '0' so we can guarantee to not reuse it while we are still on this path.
|
||||||
|
board[i][j]='0';
|
||||||
|
|
||||||
|
// general case
|
||||||
|
// if search job is still incomplete, then let's continue searching letter i in all four directions, and if we can find the word in any of these four directions, then we are good.
|
||||||
|
if( (search(board, word, i, j+1, index+1) ||
|
||||||
|
search(board, word, i, j-1, index+1) ||
|
||||||
|
search(board, word, i-1, j, index+1) ||
|
||||||
|
search(board, word, i+1, j, index+1) ) == true ){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
// restore board[i][j] to its original value
|
||||||
|
board[i][j] = c;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool exist(vector<vector<char>>& board, string word) {
|
||||||
|
int m=board.size();
|
||||||
|
int n=board[0].size();
|
||||||
|
// find the first letter of this word
|
||||||
|
for(int i=0;i<m;i++){
|
||||||
|
for(int j=0;j<n;j++){
|
||||||
|
// if it's true, return true; if not, continue search at next (i,j)
|
||||||
|
if(search(board, word, i, j, 0) == true){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if we reach here, then the word can't be found.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user