forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
44 lines (43 loc) · 1.21 KB
/
solution.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
41
42
43
44
class Solution
{
public:
vector<vector<char> > brd;
vector<vector<bool> > flag;
string wrd;
bool isExist(int row, int line, int index)
{
if(index == wrd.size())
return true;
else
{
if(row>=brd.size() || row<0 || line>= brd[0].size() || line<0)
return false;
if(flag[row][line] == true)
return false;
if(wrd[index] == brd[row][line])
{
flag[row][line] = true;
bool temp = ( isExist(row+1,line,index+1)
|| isExist(row,line+1,index+1)
|| isExist(row-1,line,index+1)
|| isExist(row,line-1,index+1));
flag[row][line] = false;
return temp;
}
}
return false;
}
bool exist(vector<vector<char> > &board, string word)
{
brd = board;
wrd = word;
flag.resize(board.size(),vector<bool>(board[0].size(),false));
for(int i=0;i<board.size();i++)
for(int j=0;j<board[i].size();j++)
{
if(isExist(i,j,0))
return true;
}
return false;
}
};