zoukankan      html  css  js  c++  java
  • 【LeetCode】79. Word Search

    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.

    For example,
    Given board =

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

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

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

    核心思想如下:

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

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

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

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

    出栈之后的节点恢复为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;
        }
    };

  • 相关阅读:
    QT学习笔记2
    QT学习笔记1
    图像中区域生长算法的详解和实现
    图像识别中的深度学习 转
    Appium基础环境搭建(windows)---基于python
    selenium实现文件上传方法汇总(AutoIt、win32GUI、sengkeys)---基于python
    selenium中的三种等待方式(显示等待WebDriverWait()、隐式等待implicitly()、强制等待sleep())---基于python
    水仙花数---基于python
    九九乘法表实现---基于python
    selenium元素定位不到之iframe---基于python
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/3747544.html
Copyright © 2011-2022 走看看