-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-search.cpp
50 lines (40 loc) · 1.29 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
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if (board.empty() || board[0].empty()) {
return false;
}
rows = board.size();
cols = board[0].size();
a.resize(rows, vector<bool>(cols, false));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == word[0] && backtrack(board, word, 0, i, j)) {
return true;
}
}
}
return false;
}
private:
int rows;
int cols;
vector<vector<bool>> a;
bool backtrack(vector<vector<char>>& board, const string& word, int index, int row, int col) {
if (index == word.length()) {
return true;
}
if (row < 0 || row >= rows || col < 0 || col >= cols || a[row][col] || board[row][col] != word[index]) {
return false;
}
a[row][col] = true;
if (backtrack(board, word, index + 1, row + 1, col) ||
backtrack(board, word, index + 1, row - 1, col) ||
backtrack(board, word, index + 1, row, col + 1) ||
backtrack(board, word, index + 1, row, col - 1)) {
return true;
}
a[row][col] = false;
return false;
}
};