-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79. Word Search.cpp
40 lines (36 loc) · 1.13 KB
/
79. Word Search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
bool dfs(vector<vector<char>>& board, string &word, int i, int j, int index, vector<vector<bool>>&vis)
{
if(index >= word.length()){
return true;
}
if(i<0 || i>=board.size() || j<0 || j>=board[0].size() || vis[i][j] == true || board[i][j] != word[index]){
return false;
}
vis[i][j] = true;
index++;
bool ans = dfs(board, word, i+1, j, index, vis)
|| dfs(board, word, i-1, j, index, vis)
|| dfs(board, word, i, j+1, index, vis)
|| dfs(board, word, i, j-1, index, vis);
vis[i][j] = false;
return ans;
}
bool exist(vector<vector<char>>& board, string word) {
int r = board.size();
int c = board[0].size();
vector<vector<bool> > vis(r,vector<bool>(c, 0));
for(int i=0; i<r; i++)
{
for(int j=0; j<c; j++)
{
int index = 0;
if(dfs(board, word, i, j, index, vis)){
return true;
}
}
}
return false;
}
};