-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword-search.cpp
35 lines (34 loc) · 1.06 KB
/
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
class Solution {
public:
vector<vector<bool>> vis;
vector<int> dx={0,0,1,-1};
vector<int> dy={1,-1,0,0};
int row,col;
bool exist(vector<vector<char>>& board, string word) {
row=board.size();
if(row==0) return false;
col=board[0].size();
if(row*col<word.size()) return false;
vis.clear();
vis.resize(row,vector<bool>(col,false));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(dfs(board,vis,word,i,j,0)) return true;
}
}
return false;
}
bool dfs(vector<vector<char>>& board, vector<vector<bool>> &vis, string &word, int x, int y, int i){
if(board[x][y]!=word[i]) return false;
if(i==word.size()-1) return true;
vis[x][y]=true;
for(int k=0;k<4;k++){
int xx=x+dx[k];
int yy=y+dy[k];
if(xx<0 || xx>=row || yy<0 || yy>=col || vis[xx][yy]) continue;
if(dfs(board,vis,word,xx,yy,i+1)) return true;
}
vis[x][y]=false;
return false;
}
};