classSolution{public:int rows;int cols;boolexist(vector<vector<char>>& board, string word){
rows = board.size();
cols = board[0].size();for(int i =0; i < rows; i++){for(int j =0; j < cols; j++){if(dfs(board, word, i, j,0)){returntrue;}}}returnfalse;}booldfs(vector<vector<char>>& board, string word,int x,int y,int index){if(x <0|| x >= rows || y <0|| y >= cols || board[x][y]!= word[index]){returnfalse;}if(index == word.length()-1){returntrue;}
board[x][y]='\0';bool result =dfs(board, word, x +1, y, index +1)||dfs(board, word, x -1, y, index +1)||dfs(board, word, x, y +1, index +1)||dfs(board, word, x , y -1, index +1);
board[x][y]= word[index];return result;}};