# LeetCode 212. 單詞搜索 II (DFS+字典樹)

$O(size*len+m*n*len)$ 如果size很大的話，這個優化是非常明顯的。

const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
class Solution {
struct Node{
bool isEnd = false;
string str = "";
Node* children[26] = {0};
};
Node* root = new Node;
void insert(const string& s){
Node* p = root;
for(char c:s){
if(!p->children[c-'a']){
p->children[c-'a'] = new Node;
}
p = p->children[c-'a'];
}
p->isEnd = true;
p->str = s;
}
public:
unordered_set<string> res;
vector<string> ans;
vector<vector<bool>> vis;
int m,n;
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
m = board.size();
n = board[0].size();
vis.resize(m+1,vector<bool>(n+1,false));
for(string& s:words){
insert(s);
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
dfs(i,j,root,board);
}
}
for(const string& s:res){
ans.push_back(s);
}
return ans;
}
void dfs(int x,int y,Node* p,vector<vector<char>>& board) {
char c = board[x][y];
Node* pp = p->children[c-'a'];
if(pp){
if(pp->isEnd){
res.insert(pp->str);
}
vis[x][y] = 1;
for(int k=0;k<4;k++){
int nx = x+dx[k];
int ny = y+dy[k];
if(nx>=0&&nx<m&&ny>=0&&ny<n && !vis[nx][ny]){
dfs(nx,ny,pp,board);
}
}
vis[x][y] = 0;
}
}
};