//单词搜索/*给出一个二维字符数组和一个单词,判断单词是否在数组中出现,
单词由相邻单元格的字母连接而成,相邻单元指的是上下左右相邻。
同一单元格的字母不能多次使用。*/#include<vector>#include<algorithm>#include<iostream>usingnamespace std;classSolution{public:boolisOut(int r,int c,int rows,int cols){return r <0|| r >= rows || c <0|| c >= cols;}boolexistChar(vector<vector<char>>&board, string& word,int r,int c,int start){if(start >= word.size())returntrue;if(isOut(r, c, board.size(), board[0].size())|| word[start]!= board[r][c])returnfalse;int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};char temp = board[r][c];
board[r][c]='.';for(int k =0; k <4;++k){if(existChar(board, word, r + dx[k], c + dy[k], start +1))returntrue;}
board[r][c]= temp;returnfalse;}boolexist(vector<vector<char>>&board, string word){for(int r =0; r < board.size();++r){for(int c =0; c < board[0].size();++c){if(board[r][c]== word[0]){if(existChar(board, word, r, c,0))returntrue;}}}returnfalse;}};
单词搜索//单词搜索/*给出一个二维字符数组和一个单词,判断单词是否在数组中出现,单词由相邻单元格的字母连接而成,相邻单元指的是上下左右相邻。同一单元格的字母不能多次使用。*/#include<vector>#include<algorithm>#include<iostream>using namespace std;class Solution {public: bool isOut(int r, int c, int rows, int col