-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathWord_Search.cpp
57 lines (50 loc) · 2.11 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
bool existUtils(vector<vector<char> > &board, string word, vector<vector<bool> > &visited, int x, int y, int indx) {
if(indx == word.length() - 1) {
return true;
}
if(y - 1 >= 0 and !visited[x][y - 1] and board[x][y - 1] == word[indx + 1]) {
visited[x][y - 1] = true;
if( existUtils(board, word, visited, x, y - 1, indx + 1) )
return true;
visited[x][y - 1] = false;
}
if(x - 1 >= 0 and !visited[x - 1][y] and board[x - 1][y] == word[indx + 1]) {
visited[x - 1][y] = true;
if( existUtils(board, word, visited, x - 1, y, indx + 1) )
return true;
visited[x - 1][y] = false;
}
if(y + 1 < board[0].size() and !visited[x][y + 1] and board[x][y + 1] == word[indx + 1]) {
visited[x][y + 1] = true;
if( existUtils(board, word, visited, x, y + 1, indx + 1) )
return true;
visited[x][y + 1] = false;
}
if(x + 1 < board.size() and !visited[x + 1][y] and board[x + 1][y] == word[indx + 1]) {
visited[x + 1][y] = true;
if( existUtils(board, word, visited, x + 1, y, indx + 1) )
return true;
visited[x + 1][y] = false;
}
return false;
}
bool exist(vector<vector<char> > &board, string word) {
for(int i = 0; i < board.size(); ++i) {
for(int j = 0; j < board[i].size(); ++j) {
if(board[i][j] == word[0]) {
if(word.size() == 1) {
return true;
} else {
vector<vector<bool> > visited(board.size(), vector<bool>(board[0].size(), false));
visited[i][j] = true;
if(existUtils(board, word, visited, i, j, 0))
return true;
}
}
}
}
return false;
}
};