|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +class Solution { |
| 5 | +public: |
| 6 | + |
| 7 | + bool isfullyfound(int r, int c, int index, vector<vector<char>>& board, vector<vector<bool>>&visited, string word){ |
| 8 | + if(index == word.size()){ |
| 9 | + return true; |
| 10 | + } |
| 11 | + if(r>=0 && r<board.size() && c>=0 && c<board[0].size() && !visited[r][c] && word[index] == board[r][c]){ |
| 12 | + visited[r][c] = true; |
| 13 | + if( isfullyfound(r-1,c,index+1,board,visited,word) || |
| 14 | + isfullyfound(r+1,c,index+1,board,visited,word) || |
| 15 | + isfullyfound(r,c-1,index+1,board,visited,word) || |
| 16 | + isfullyfound(r,c+1,index+1,board,visited,word) || |
| 17 | + isfullyfound(r+1,c+1,index+1,board,visited,word) || |
| 18 | + isfullyfound(r+1,c-1,index+1,board,visited,word) || |
| 19 | + isfullyfound(r-1,c-1,index+1,board,visited,word) || |
| 20 | + isfullyfound(r-1,c+1,index+1,board,visited,word) |
| 21 | + )return true; |
| 22 | + |
| 23 | + visited[r][c] = false; |
| 24 | + } |
| 25 | + return false; |
| 26 | + } |
| 27 | + bool wordfound(string word,vector<vector<char>>& board){ |
| 28 | + int index = 0; |
| 29 | + vector<vector<bool>>visited(board.size(), vector<bool>(board[0].size(), false)); |
| 30 | + for(int i = 0;i<board.size();i++){ |
| 31 | + for(int j = 0;j<board[0].size();j++){ |
| 32 | + if(word[index] == board[i][j]){ |
| 33 | + if(isfullyfound(i,j,index,board,visited,word)){ |
| 34 | + return true; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + return false; |
| 40 | + |
| 41 | + } |
| 42 | + vector<string> wordBoggle(vector<vector<char> >& board, vector<string>& dictionary) { |
| 43 | + vector<string>v; |
| 44 | + for(auto i: dictionary){ |
| 45 | + if(wordfound(i,board)){ |
| 46 | + v.push_back(i); |
| 47 | + } |
| 48 | + } |
| 49 | + return v; |
| 50 | + } |
| 51 | +}; |
| 52 | + |
0 commit comments