-
Notifications
You must be signed in to change notification settings - Fork 7
/
212.cpp
56 lines (51 loc) · 1.72 KB
/
212.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
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
if(board.empty() || words.empty()) return vector<string>();
TrieNode *root = new TrieNode(),*nd;
for(auto str: words)
{
nd = root;
for(auto ch: str)
{
if(!nd->next[ch-'a']) nd->next[ch-'a'] = new TrieNode();
nd = nd->next[ch-'a'];
}
nd->word = true;
}
vector<string> res;
for(int i = 0;i < board.size();i++){
for(int j = 0;j < board[0].size();j++){
dfs(board,root->next[board[i][j]-'a'],i,j,"",res);
}
}
return res;
}
private:
struct TrieNode
{
vector<TrieNode*> next;
bool word;
TrieNode(): next(vector<TrieNode*>(26, NULL)), word(false){}
};
void dfs(vector<vector<char>>& board,TrieNode *nd,int x,int y,string word,vector<string> &res){
if(!nd || board[x][y] == '#')
return;
char c = board[x][y];
word += c;
if(nd->word){
res.push_back(word);
nd->word = false; //防止重复
}
board[x][y] = '#';
if(x - 1 >= 0)
dfs(board,nd->next[board[x - 1][y] - 'a'],x - 1,y,word,res);
if(x + 1 < board.size())
dfs(board,nd->next[board[x + 1][y] - 'a'],x + 1,y,word,res);
if(y - 1 >= 0)
dfs(board,nd->next[board[x][y - 1] - 'a'],x,y - 1,word,res);
if(y + 1 < board[0].size())
dfs(board,nd->next[board[x][y + 1] - 'a'],x,y + 1,word,res);
board[x][y] = c;
}
};