class Solution { public: bool dfs(vector<vector<char>>& board,string word,int i,int j,int n){ //递归终止条件 if(n==word.size()) return true; if(i<0 || j<0 || i>=board.size()||j>=board[0].size() || board[i][j]!=word[n]) return false; board[i][j]='0';//这个位置检索到了 bool flag=(dfs(board,word,i+1,j,n+1) || dfs(board,word,i-1,j,n+1)|| dfs(board,word,i,j+1,n+1)|| dfs(board,word,i,j-1,n+1)); board[i][j]=word[n]; return flag; } bool exist(vector<vector<char>>& board, string word) { //深度优先递归回溯 if(word=="") return false; int m=board.size(),n=board[0].size(); for(int i=0;i<m;++i){ for(int j=0;j<n;++j){ if(dfs(board,word,i,j,0)) return true; } } return false; } };