-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79. Word Search.cpp
63 lines (44 loc) · 1.71 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution {
int dirx[4] = {-1,1,0,0};
int diry[4] = {0,0,1,-1};
bool recurse(vector<vector<char>>& board, string &word, int i, int j ,const string &myword,
int n,vector<vector<int>> &visited) {
// cout << myword << " " << i << " " << j << " " << endl;
if (myword == word)
return true;
if(myword.length() > word.length())
return false;
if(myword[myword.length()-1]!=word[myword.length()-1])
return false;
visited[i][j] = 1;
for(int k = 0;k<4;++k) {
int newx = i + dirx[k];
int newy = j + diry[k];
if (newx >=0 && newx < board.size() && newy >=0 && newy < board[0].size() && !visited[newx][newy]) {
visited[newx][newy] = 1;
if (recurse(board,word,newx,newy, myword+board[newx][newy],n+1,visited))
return true;
visited[newx][newy] = 0;
}
}
return false;
}
public:
bool exist(vector<vector<char>>& board, string word) {
if(board.empty() && word.length() == 0)
return true;
else if(board.empty() || word.length() == 0 || word.length() > board.size()*board[0].size())
return false;
string cur = "";
for(int i = 0;i<board.size();++i) {
for(int j = 0;j<board[0].size();++j) {
if(board[i][j] == word[0]) {
vector<vector<int>> visited(board.size(), vector<int> (board[0].size(),0));
if(recurse(board,word,i,j,cur + word[0],0,visited))
return true;
}
}
}
return false;
}
};