-
Notifications
You must be signed in to change notification settings - Fork 481
/
0212.cpp
67 lines (60 loc) · 1.58 KB
/
0212.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
class Node
{
public:
Node() : wordId(-1) {}
~Node()
{
for (const auto& item : next)
delete item.second;
}
unordered_map<char, Node*> next;
int wordId;
};
class Solution
{
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words)
{
r = board.size(), c = board[0].size();
Node* root = new Node();
for (int i = 0; i < words.size(); i++)
{
auto node = root;
for (auto ch : words[i])
{
if (!node->next.count(ch)) node->next[ch] = new Node();
node = node->next[ch];
}
node->wordId = i;
}
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
dfs(i, j, root, board, words);
}
}
return res;
}
private:
int r, c;
vector<string> res;
const int dire[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
void dfs(int i, int j, Node* node, vector<vector<char>>& board, vector<string>& words)
{
if (i < 0 || i >= r || j < 0 || j >= c ||
!node->next.count(board[i][j])) return;
auto tmp = board[i][j];
board[i][j] = '$';
node = node->next[tmp];
if (node->wordId != -1) {
res.push_back(words[node->wordId]);
node->wordId = -1;
}
for (auto& it : dire) {
int nx = it[0] + i, ny = it[1] + j;
dfs(nx, ny, node, board, words);
}
board[i][j] = tmp;
}
};