Fork me on GitHub

79. 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 =

[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

解析

  • 这道题很容易感觉出来是图的题目,其实本质上还是做深度优先搜索。基本思路就是从某一个元素出发,往上下左右深度搜索是否有相等于word的字符串。这里注意每次从一个元素出发时要重置访问标记(也就是说虽然单次搜索字符不能重复使用,但是每次从一个新的元素出发,字符还是重新可以用的)。
// 79. Word Search
class Solution_79 {
public:

	typedef pair<int, int> pii;
	int ajd[8][2] = { { -1, -1 }, { 0, -1 }, { 1, -1 }, { -1, 0 }, { 1, 0 }, { -1, 1 }, { 0, 1 }, {1,1} }; //上下左右,四连通域

	bool judgeValid_bug(vector<vector<char>>& board, string word,int index, int x, int y)
	{
		int m = board.size();
		int n = board[0].size();

		pii p(x,y);
		queue<pii> que;
		que.push(p);

		if (index == word.size())
		{
			return true;
		}
		while (!que.empty())
		{
			int size = que.size();
			while (size--)
			{
				pii temp = que.front();
				x = temp.first;
				y = temp.second;
				que.pop();

				for (int i = 0; i < 8; i++)
				{
					if (ajd[i][0] + x >= 0 && (ajd[i][0] + x)<m && (ajd[i][1] + y) >= 0 && (ajd[i][1] + y)<n)
					{
						if (board[ajd[i][0] + x][ajd[i][1] + y] == word[index]) //未被归域下,才放入
						{
							temp.first = ajd[i][0] + x;
							temp.second = ajd[i][1] + y;
							que.push(temp);
							if (index == word.size() - 1)
							{
								return true;
							}
						}
					}
				}

			}
			index++;
		}
		return false;
	}

	// 上下左右 dfs
	bool judgeValid(vector<vector<char>>& board, string word, int index, int x, int y)
	{
		int m = board.size();
		int n = board[0].size();

		if (index >= word.size())
		{
			return true;
		}
		if (x < 0 || y < 0 || x >= m || y >= n)
		{
			return false; //超出边界
		}

		if (board[x][y]!=word[index])
		{
			return false;
		}

		char temp = board[x][y];
		board[x][y] = '.';  //节约used[i][j] = true; 空间 //防止从同一位置开始,以后重复使用

		bool ret = judgeValid(board,word,index+1,x-1,y)||
			judgeValid(board,word,index+1,x,y-1)||
			judgeValid(board,word,index+1,x,y+1)||
			judgeValid(board,word,index+1,x+1,y);

		board[x][y] = temp;

		return ret;
	}

	bool exist(vector<vector<char>>& board, string word) {
		
		if (board.empty())
		{
			return false;
		}
		if (word.empty())
		{
			return true;
		}
		int m = board.size();
		int n = board[0].size();

		for (int i = 0; i < m;i++)
		{
			for (int j = 0; j < n;j++)
			{
				if (board[i][j]==word[0])
				{
					if (judgeValid(board,word,0,i,j))
					{
						return true;
					}
				}
			}
		}

		return false;
	}


};

题目来源

posted @ 2018-04-02 16:57  ranjiewen  阅读(195)  评论(0编辑  收藏  举报