079 Word Search[Leetcode]

来源:互联网 发布:网络协议的作用 编辑:程序博客网 时间:2024/06/05 19:15

题目描述

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