forked from geemaple/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79.word-search.cpp
69 lines (57 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
64
65
66
67
68
69
class Solution {
private:
int directions[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
int convert_point(vector<vector<char>>& board, int x, int y)
{
return x * board[0].size() + y;
}
bool check_in_board(vector<vector<char>>& board, int x, int y)
{
return (x >= 0 && x < board.size() && y >= 0 && y < board[x].size());
}
bool dfs_search(vector<vector<char>>& board, int x, int y, unordered_set<int>& visted, string &word, int index)
{
if (board[x][y] != word[index])
{
return false;
}
if (index == word.size() - 1)
{
return true;
}
int point = convert_point(board, x, y);
visted.insert(point);
for (auto it: directions)
{
int new_x = x + it[0];
int new_y = y + it[1];
int new_point = convert_point(board, new_x, new_y);
if (check_in_board(board, new_x, new_y) && visted.count(new_point) == 0)
{
if(dfs_search(board, new_x, new_y, visted, word, index + 1))
{
return true;
}
}
}
visted.erase(point);
return false;
}
public:
bool exist(vector<vector<char>>& board, string word) {
unordered_set<int> visted;
for (int i = 0; i < board.size(); ++i)
{
vector<char> row = board[i];
for (int j = 0; j < row.size(); ++j)
{
bool result = dfs_search(board, i, j, visted, word, 0);
if (result)
{
return true;
}
}
}
return false;
}
};