leetcode-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.

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.

给定一个二维网格和一个单词,找出该单词是否存在于网格中。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

示例:

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

给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.

思路:贪心算法。从每个位置开始都向上下左右 走,然后递归走 判断是否每个 位置元素是否 相等。这里如果 借助新的辅助空间的话基本上会超时。所以用一个临时 变量记录当前遍历到的值,遍历完之后恢复。

class Solution {
public:
    bool search(vector<vector<char>> &board,string word,int i,int x,int y)
    {
        if(i == word.size()) return true;
        int m=board.size(),n=board[0].size();
        if(x>=m || y>=n || x<0 || y<0 || board[x][y]!=word[i]) return false;
        char c = board[x][y];
        board[x][y] = '.';
        bool res = search(board,word,i+1,x-1,y) 
                || search(board,word,i+1,x+1,y)
                || search(board,word,i+1,x,y-1)
                || search(board,word,i+1,x,y+1);
        board[x][y] = c;
        return res;        
    }
    bool exist(vector<vector<char>>& board, string word) {
        if(!board.size() || !board[0].size()) return false;
        int m=board.size(),n=board[0].size();
        for(int i=0;i<m;++i)
        {
            for(int j=0;j<n;++j)
                if(search(board,word,0,i,j)) return true;
        }
        return false;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/87886717