| 3 | using namespace std; |
| 4 | |
| 5 | bool solve(vector<vector<char>> &board, string &word, int index, int x, int y) { |
| 6 | |
| 7 | if(index == word.length()) { |
| 8 | return true; |
| 9 | } |
| 10 | |
| 11 | if(x < 0 || y < 0 || x >= board.size() || y >= board[0].size() || board[x][y] == '*') |
| 12 | return false; |
| 13 | |
| 14 | if(board[x][y] != word[index]) |
| 15 | return false; |
| 16 | |
| 17 | char temp = board[x][y]; |
| 18 | board[x][y] = '*'; |
| 19 | |
| 20 | if ( |
| 21 | solve(board, word, index+1, x-1, y) || |
| 22 | solve(board, word, index+1, x+1, y) || |
| 23 | solve(board, word, index+1, x, y-1) || |
| 24 | solve(board, word, index+1, x, y+1) |
| 25 | ) { |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | board[x][y] = temp; |
| 30 | |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | bool exist(vector<vector<char>> &board, string word) { |
| 35 | |