diff --git a/lectures/12_advanced_recursion/README.md b/lectures/12_advanced_recursion/README.md index 24e7e84..ba2a057 100644 --- a/lectures/12_advanced_recursion/README.md +++ b/lectures/12_advanced_recursion/README.md @@ -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.) - 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. - – 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). - – At each such location, the occurrences of the second letter are sought in the 4 locations surrounding (r, c). - - At each location where the second letter is found, a search is initiated in the 4 locations surrouding this second letter. + – 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 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 location where the first letter is found, a search of the second letter is initiated in the 4 neighboring locations of location (r, c). + - 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 -- [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. ## 12.9 Summary of Nonlinear Word Search Recursion diff --git a/leetcode/p79_wordsearch.cpp b/leetcode/p79_wordsearch.cpp new file mode 100644 index 0000000..3a293c7 --- /dev/null +++ b/leetcode/p79_wordsearch.cpp @@ -0,0 +1,58 @@ +class Solution { +public: + // search word[index] at locations (i,j) + bool search(vector>& 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>& board, string word) { + int m=board.size(); + int n=board[0].size(); + // find the first letter of this word + for(int i=0;i