LeetCode_Word Search

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;
		}
	}
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值