【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;
    }
};

相關文章
相關標籤/搜索