zoukankan      html  css  js  c++  java
  • 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;
    	}
    
    
    };
    

    题目来源

  • 相关阅读:
    mysql too many connections 解决方法
    finereport激活码
    oracle删除重复数据只保留一条
    line -1: Validation of SOAP-Encoded messages not supported
    JSONObject和JSONArray区别及基本用法
    eclipse批量修改package、import中的包名
    如何去掉ArrayList重复的id
    jquery.dataTables.min.js: Uncaught TypeError: Cannot read property 'style' of undefined
    Java截取最后一个 _ 后面的所有字符
    Java线程面试题 Top 50(转载)
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/8695212.html
Copyright © 2011-2022 走看看