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.
For example,
Given board =
[ ["ABCE"], ["SFCS"], ["ADEE"] ]word =
"ABCCED"
, -> returns
true
,
word =
"SEE"
, -> returns
true
,
word = "ABCB"
, -> returns false
.
先附上我的bfs超时代码。
struct state{
vector <vector<int>> path;
int curx;
int cury;
int wPos;
state(int boardy,int boardx,int i,int j,int wp)
:path(vector <vector<int>> (boardy,vector <int>(boardx,0))),curx(i),cury(j),wPos(wp){};
state(const state& s):path(s.path),curx(s.curx),cury(s.cury),wPos(s.wPos){};
state & operator= (const state &s){
if(&s!=this){
path=s.path;
curx=s.curx;
cury=s.cury;
wPos=s.wPos;
}
return *this;
}
};
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
int size=word.size();
if(size==0) return true;
int xSize=board.size();
if(!xSize) return false;
int ySize=board[0].size();
if(!ySize) return false;
if(size>xSize*ySize) return false;
queue <state> bfsQueue;
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
state temp(board.size(),board[0].size(),i,j,0);
if(board[i][j]==word[0]){
temp.path[i][j]=1;
temp.wPos=1;
bfsQueue.push(temp);
while(!bfsQueue.empty()){
temp=bfsQueue.front();
bfsQueue.pop();
if(temp.wPos==word.size()){
return true;
}
int curx=temp.curx;
int cury=temp.cury;
int wPos=temp.wPos;
//up
state tempUp=temp;
if(curx+1<board.size()&&board[curx+1][cury]==word[wPos]&&temp.path[curx+1][cury]==0){
tempUp.path[curx+1][cury]=1;
tempUp.cury=cury;
tempUp.curx=curx+1;
tempUp.wPos++;
bfsQueue.push(tempUp);
}
//down
state tempdown=temp;
if(curx-1>=0&&board[curx-1][cury]==word[wPos]&&temp.path[curx-1][cury]==0){
tempdown.path[curx-1][cury]=1;
tempdown.cury=cury;
tempdown.curx=curx-1;
tempdown.wPos++;
bfsQueue.push(tempdown);
}
//right
state tempright=temp;
if(cury+1<board[0].size()&&board[curx][cury+1]==word[wPos]&&temp.path[curx][cury+1]==0){
tempright.path[curx][cury+1]=1;
tempright.cury=cury+1;
tempright.curx=curx;
tempright.wPos++;
bfsQueue.push(tempright);
}
//left
state templeft=temp;
if(cury-1>=0&&board[curx][cury-1]==word[wPos]&&temp.path[curx][cury-1]==0){
templeft.path[curx][cury-1]=1;
templeft.cury=cury-1;
templeft.curx=curx;
templeft.wPos++;
bfsQueue.push(templeft);
}
}
}
}
}
return false;
}
};
AC的dfs算法:
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
int size=word.size();
if(size==0) return true;
int xSize=board.size();
if(!xSize) return false;
int ySize=board[0].size();
if(!ySize) return false;
if(size>xSize*ySize) return false;
this->boardvect=board;
this->wordstr=word;
vector <vector <bool>> ifused(xSize,vector <bool>(ySize,0 ));
for(int i=0;i<xSize;i++){
for(int j=0;j<ySize;j++){
if(dfs(i,j,0,ifused)){
return true;
}
}
}
return false;
}
private:
vector <vector<char> > boardvect;//成员变量主要为了减少参数个数
string wordstr;//成员变量主要为了减少参数个数
bool dfs(int posx,int posy,int wPos,vector <vector <bool>> &ifused){
if(wPos==wordstr.size()) return true;
if(isInBoard(posx,posy)&&ifused[posx][posy]==false
&&boardvect[posx][posy]==wordstr[wPos]){
ifused[posx][posy]=true;
if(dfs(posx+1,posy,wPos+1,ifused)){
return true;
}
else{
if(dfs(posx,posy+1,wPos+1,ifused)){
return true;
}
else{
if(dfs(posx-1,posy,wPos+1,ifused)){
return true;
}
else{
if(dfs(posx,posy-1,wPos+1,ifused)){
return true;
}
}
}
}
ifused[posx][posy]=false;
}
return false;
}
bool isInBoard(int posx,int posy){
//check x
if(posx>=0&&posx<boardvect.size()){
//check y
if(posy>=0&&posy<boardvect[0].size()){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
};