Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example:
board =[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E']]Given word = "ABCCED", return true.Given word = "SEE", return true.Given word = "ABCB", return false.
code1
class Solution{public: bool exist(vector>& board, string word) { if(board.empty()||word.empty()||board.size()*board.at(0).size() > flag(board.size(),vector (board.at(0).size(),false)); for(int i=0;i > &flag,const vector > &board,const string &word) { if(len==word.length()) return true; if(flag.at(x).at(y)) return false; flag.at(x).at(y)=true; for(int *coor:coordinate) { int _x=coor[0]+x; int _y=coor[1]+y; if(_x>=0&&_x =0&&_y
code2
class Solution{public: bool exist(vector>& board, string word) { if(board.empty()||word.empty()||board.size()*board.at(0).size() > &board,const char *word) { if(x<0|x>=board.size()||y<0||y>=board.at(0).size()||board.at(x).at(y)=='\0'||*word!=board.at(x).at(y)) return false; if(*(word+1)=='\0') return true; char c=board.at(x).at(y); board.at(x).at(y)='\0'; if(exist_core(x+1,y,board,word+1)||exist_core(x-1,y,board,word+1)|| exist_core(x,y+1,board,word+1)||exist_core(x,y-1,board,word+1)) return true; board.at(x).at(y)=c; return false; }};