https://leetcode-cn.com/problems/word-search/
单词搜索,真的是入门级的dfs了,因为想剪枝,导致bug改不过…
还是最low的方法过的,嘤嘤嘤
怎么肥事…
看了一部分的题解,如果刚开始调用dfs的时候,st = 1 的话
就一定是当下dfs(1, x + direct[k][0], y + direct[k][1])四种情况一起判断了,不能直接调用dfs…
class Solution {
public:
bool dfs(vector<vector<char>>& board, string word,vector<vector<int>>& vis, int st, int x,int y){
if(st == word.length())
return true;
if(x < 0 || y < 0 || x >= board.size() || y >= board[0].size() || vis[x][y] || word[st] != board[x][y])
return false;
vis[x][y] = 1;
bool ans = dfs(board,word,vis,st+1,x+1,y) || dfs(board,word,vis,st+1,x,y+1) || dfs(board,word,vis,st+1,x-1,y)|| dfs(board,word,vis,st+1,x,y-1);
vis[x][y] = 0;
return ans;
}
bool exist(vector<vector<char>>& board, string word) {
vector<vector<int>> vis(board.size(), vector<int>(board[0].size(), 0));
for(int i = 0;i < board.size(); i++){
for(int j = 0;j < board[i].size(); j++){
'''if(board[i][j] == word[0] && dfs(board, word, vis, 1, i, j))是个bug,暂时改不过,为什么呢'''
if(dfs(board, word, vis, 0, i, j))
return true;
}
}
return false;
}
};