079 Word Search[Leetcode]

题目描述

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.

思路:和迷宫问题类似,在当前位置搜索下一个字母时,向四个方向查找,注意需要保存一个数组记录节点是否已被访问过。

用递归方法思路比较清晰,代码如下,运行时间为40ms。

class Solution {
private:
    vector<vector<bool>> visited;
public:
    bool exist(vector<vector<char>>& board, string word) {
        if(word.size() == 0)
            return true;
        if(board.size() == 0 || board[0].size() == 0)
            return false;

        int row(board.size()), col(board[0].size());
        if(word.size() > row*col)
            return false;

        visited = vector<vector<bool>>(row, vector<bool>(col, false));

        for(int i = 0; i < row; ++i) {
            for(int j = 0; j < col; ++j) {
                if(board[i][j] == word[0] && search(board, i, j, word, 0))
                    return true;
            }
        }
        return false;
    }

    bool search(vector<vector<char>> &board, int r, int c, string &word, int index) {
        if(r >= board.size() || r < 0 || c >= board[0].size() || c < 0) 
            return false;

        if(visited[r][c] || board[r][c] != word[index])
            return false;

        if(index == word.size() - 1)
            return true;

        visited[r][c] = true;
        if(search(board, r, c+1, word, index+1) || search(board, r+1, c, word, index+1) || search(board, r-1, c, word, index+1) || search(board, r, c-1, word, index+1))
            return true;
        visited[r][c] = false;
        return false;
    }
};

非递归方法:
利用栈来存储路径,并需要记录上一次访问的方向,在这里设计了一个数据结构,用direction提示下一次要搜索的方向。如果都不符合,则回退。运行时间为132ms。代码如下:

struct path {
    int row;
    int col;
    int direction;

    path(int r, int c) : row(r), col(c), direction(0) {}
    path() : row(0), col(0), direction(0) {}
};

class Solution {
private:
    vector<vector<bool>> visited;
public:
    bool exist(vector<vector<char>>& board, string word) {
        if(word.size() == 0)
            return true;
        if(board.size() == 0 || board[0].size() == 0)
            return false;

        int row(board.size()), col(board[0].size());
        if(word.size() > row*col)
            return false;

        visited = vector<vector<bool>>(row, vector<bool>(col, false));

        for(int i = 0; i < row; ++i) {
            for(int j = 0; j < col; ++j) {
                if(board[i][j] == word[0] && stackSearch(board, i, j, word))
                    return true;
            }
        }
        return false;
    }

    bool stackSearch(vector<vector<char>> &board, int r, int c, string &word) {
        if(word.size() == 1)
            return true;

        stack<path> paths;
        int row(board.size()), col(board[0].size()), index(1);
        paths.push(path(r, c));
        visited[r][c] = true;

        while(!paths.empty()) {
            int tr(paths.top().row), tc(paths.top().col);
            //top
            if(paths.top().direction == 0) {
                paths.top().direction = 1;
                if(tr != 0 && !visited[tr-1][tc] && board[tr-1][tc] == word[index]) {
                    paths.push(path(tr-1, tc));
                    visited[tr-1][tc] = true;
                    ++index;
                    if(index == word.size())
                        return true;
                    continue;
                }
            }
            //right
            else if(paths.top().direction == 1) {
                paths.top().direction = 2;
                if(tc != col-1 && !visited[tr][tc+1] && board[tr][tc+1] == word[index]) {
                    paths.push(path(tr, tc+1));
                    visited[tr][tc+1] = true;
                    ++index;
                    if(index == word.size())
                        return true;
                    continue;
                }
            }
            //down
            else if(paths.top().direction == 2) {
                paths.top().direction = 3;
                if(tr != row-1 && !visited[tr+1][tc] && board[tr+1][tc] == word[index]) {
                    paths.push(path(tr+1, tc));
                    visited[tr+1][tc] = true;
                    ++index;
                    if(index == word.size())
                        return true;
                    continue;
                }
            }
            //left
            else if(paths.top().direction == 3) {
                paths.top().direction = 4;
                if(tc != 0 && !visited[tr][tc-1] && board[tr][tc-1] == word[index]) {
                    paths.push(path(tr, tc-1));
                    visited[tr][tc-1] = true;
                    ++index;
                    if(index == word.size())
                        return true;
                    continue;
                }
            }
            //back
            else {
                visited[tr][tc] = false;
                paths.pop();
                --index;
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值