-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSearch2.cpp
More file actions
75 lines (72 loc) · 2.06 KB
/
WordSearch2.cpp
File metadata and controls
75 lines (72 loc) · 2.06 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
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
70
71
72
73
74
75
class node{
public:
vector<node*> branch;
bool end;
node(){
branch = vector<node*>(26,NULL);
end = false;
}
};
class Solution {
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
m = board.size();
n = board[0].size();
snapshot = board;
root = new node();
vector<string> dic;
string tmp;
unordered_set<string> us;
vector<string> res;
for (auto x : words)insert(x);
for (int i = 0 ; i < m ; i++){
for (int j = 0 ; j < n; j++){
tmp.clear();
board = snapshot;
searchBoard(us,tmp,board,root,i,j);
}
}
for (auto x : us)res.push_back(x);
return res;
}
void insert(string word) {
int i = 0 ;
node* cur = root;
while (i < word.size()){
int idx = word[i++]-'a';
if (cur->branch[idx]){
cur = cur->branch[idx];
}
else {
cur->branch[idx] = new node();
cur = cur->branch[idx];
}
if ( i == word.size()) cur->end = true;
}
}
int m;
int n;
node* root;
vector<vector<char>> snapshot;
void searchBoard(unordered_set<string>& res, string& tmp,vector<vector<char>>& board,node* cur,int i, int j){
if (board[i][j] == '0') return;
char ch = board[i][j];
int index = ch - 'a';
if (!cur->branch[index]) return;
tmp.push_back(ch);
if (cur->branch[index]->end)res.insert(tmp);
tmp.pop_back();
vector<pair<int,int>> dir = {{0,1},{1,0},{0,-1},{-1,0}};
for (auto x : dir){
int nx = i + x.first;
int ny = j + x.second;
if (nx < m && nx >=0 && ny < n && ny >=0 ){
tmp.push_back(ch);
board[i][j] = '0';
searchBoard(res,tmp,board,cur->branch[index],nx,ny);
board[i][j] = ch;
tmp.pop_back();
}
}
}
};