Repository navigation
Expand file tree
/
Copy path0079.cpp
More file actions
34 lines (34 loc) · 1.4 KB
/
Copy path0079.cpp
File metadata and controls
34 lines (34 loc) · 1.4 KB
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
class Solution {
private:
bool verifyExistence(vector<vector<char>> const &board, vector<vector<bool>> &visited, string ¤t, string const &target, int i, int j) {
if (current.length() == target.length()) return true;
if (board[i][j] == target[current.length()]) {
current.push_back(board[i][j]);
visited[i][j] = true;
if (current == target) return true;
vector<pair<int,int>> offsets = {{-1,0}, {0,1}, {1,0}, {0,-1}};
for (auto offset : offsets) {
int ni = i + offset.first, nj = j + offset.second;
if (ni >= 0 && ni < board.size() && nj >= 0 && nj < board[i].size() && !visited[ni][nj]) {
if (verifyExistence(board, visited, current, target, ni, nj))
return true;
}
}
current.pop_back();
visited[i][j] = false;
}
return false;
}
public:
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++) {
vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
string current = "";
if (verifyExistence(board, visited, current, word, i, j))
return true;
}
}
return false;
}
};