【LeetCode】79. Word Search

Word Searchhtml

Given a 2D board and a word, find if the word exists in the grid.node

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

For example,
Given board =3d

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]

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

 

回溯法(关于回溯法和深度优先遍历的异同),并且递归在leetcode里基本上是TLE的,因此如下就是非递归的回溯。code

核心思想以下:htm

用栈记录当前搜索的路径。blog

栈存放的节点包括4个成员: 字符c, x,y坐标,已遍历方向p。递归

注意p在回溯法中是很是重要的,用来记录已遍历过的方向(按照上下左右的顺序),否则的话就会出现无限循环的同一节点进栈出栈。leetcode

进栈以后的节点置为'*',以避免同一节点屡次进栈。

出栈以后的节点恢复为word[wind]。

 

struct Node
{
    char c;
    int x;
    int y;
    int p;    //next trial is 0-up, 1-down, 2-left, 3-right
    Node(char newc, int newx, int newy, int newp): c(newc), x(newx), y(newy), p(newp) {}
};

class Solution {
public:
    bool exist(vector<vector<char> > &board, string word) {
        if(board.empty() || board[0].empty())
            return false;
        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])
                {// maybe a success
                    stack<Node> stk;
                    Node curnode(word[0], i, j, 0);
                    stk.push(curnode);
                    board[curnode.x][curnode.y] = '*';
                    int wind = 1;
                    if(wind == word.size())
                        return true;
                    while(!stk.empty())
                    {
                        if(stk.top().p == 0)
                        {
                            stk.top().p = 1;
                            if(stk.top().x > 0 && board[stk.top().x-1][stk.top().y] == word[wind])
                            {
                                Node nextnode(word[wind], stk.top().x-1, stk.top().y, 0);
                                stk.push(nextnode);
                                board[nextnode.x][nextnode.y] = '*';
                                wind ++;
                                if(wind == word.size())
                                    return true;
                                continue;
                            }
                        }
                        if(stk.top().p == 1)
                        {
                            stk.top().p = 2;
                            if(stk.top().x < m-1 && board[stk.top().x+1][stk.top().y] == word[wind])
                            {
                                Node nextnode(word[wind], stk.top().x+1, stk.top().y, 0);
                                stk.push(nextnode);
                                board[nextnode.x][nextnode.y] = '*';
                                wind ++;
                                if(wind == word.size())
                                    return true;
                                continue;
                            }
                        }
                        if(stk.top().p == 2)
                        {
                            stk.top().p = 3;
                            if(stk.top().y > 0 && board[stk.top().x][stk.top().y-1] == word[wind])
                            {
                                Node nextnode(word[wind], stk.top().x, stk.top().y-1, 0);
                                stk.push(nextnode);
                                board[nextnode.x][nextnode.y] = '*';
                                wind ++;
                                if(wind == word.size())
                                    return true;
                                continue;
                            }
                        }
                        if(stk.top().p == 3)
                        {
                            stk.top().p = 4;
                            if(stk.top().y < n-1 && board[stk.top().x][stk.top().y+1] == word[wind])
                            {
                                Node nextnode(word[wind], stk.top().x, stk.top().y+1, 0);
                                stk.push(nextnode);
                                board[nextnode.x][nextnode.y] = '*';
                                wind ++;
                                if(wind == word.size())
                                    return true;
                                continue;
                            }
                        }
                        //restore
                        board[stk.top().x][stk.top().y] = stk.top().c;
                        stk.pop();
                        wind --;
                    }
                }
            }
        }
        return false;
    }
};

相关文章
相关标签/搜索