-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLeetCode#65.cc
52 lines (44 loc) · 1.59 KB
/
LeetCode#65.cc
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
41
42
43
44
45
46
47
48
49
50
51
52
class Solution {
private:
int n;
int m;
bool dfs(int x,int y,int cnt,vector<vector<int> > &col,
vector<vector<char> > &board,string& word){
if(cnt==word.length()) return true;
if(x<0||x>=n || y <0 || y>=m) return false;
if(col[x][y]==1 || word[cnt]!=board[x][y]) return false;
col[x][y]=1;
bool ret = false;
// up
ret = dfs(x-1,y,cnt+1,col,board,word);
if(ret) return true;
//down
ret = dfs(x+1,y,cnt+1,col,board,word);
if(ret) return true;
//left
ret = dfs(x,y-1,cnt+1,col,board,word);
if(ret) return true;
//right
ret=dfs(x,y+1,cnt+1,col,board,word);
if(ret) return true;
col[x][y]=0;
return false;
}
public:
bool exist(vector<vector<char> > &board, string word) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
n = board.size();
if(n==0) return false;
m = board[0].size();
if(m==0) return false;
vector<vector<int> > col;
for(int i=0;i<n;i++) col.push_back(vector<int>(m,0));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
bool ret = dfs(i,j,0,col,board,word);
if(ret) return true;
}
return false;
}
};