zoukankan      html  css  js  c++  java
  • 【数组】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.

    思路:

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

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

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

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

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

    /**
     * @param {character[][]} board
     * @param {string} word
     * @return {boolean}
     */
    var exist = function(board, word) {
        if(board.length==0){
            return false;
        }
        var m=board.length,n=board[0].length;
        if(m*n<word.length){
            return false;
        }
        
        for(var i=0;i<m;i++){
            for(var j=0;j<n;j++){
                if(board[i][j]==word[0]){
                    var stack=[];
                    var node={
                        c:word[0],
                        x:i,
                        y:j,
                        p:0
                    };
                    stack.push(node);
                    board[i][j]='*';
                    var wind=1;
                    if(wind==word.length){
                        return true;
                    }
                    while(stack.length!=0){
                        var top=stack[stack.length-1]
                        if(top.p==0){
                            top.p=1;
                            if(top.x>0&&board[top.x-1][top.y]==word[wind]){
                                var node={
                                    c:word[wind],
                                    x:top.x-1,
                                    y:top.y,
                                    p:0
                                };
                                stack.push(node);
                                board[node.x][node.y]='*';
                                wind++;
                                if(wind==word.length){
                                    return true;
                                }
                                continue;
                            }
                        }
                        if(top.p==1){
                            top.p=2;
                            if(top.x<m-1&&board[top.x+1][top.y]==word[wind]){
                                var node={
                                    c:word[wind],
                                    x:top.x+1,
                                    y:top.y,
                                    p:0
                                };
                                stack.push(node);
                                board[node.x][node.y]='*';
                                wind++;
                                if(wind==word.length){
                                    return true;
                                }
                                continue;
                            }
                        }
                        if(top.p==2){
                            top.p=3;
                            if(top.y>0&&board[top.x][top.y-1]==word[wind]){
                                var node={
                                    c:word[wind],
                                    x:top.x,
                                    y:top.y-1,
                                    p:0
                                };
                                stack.push(node);
                                board[node.x][node.y]='*';
                                wind++;
                                if(wind==word.length){
                                    return true;
                                }
                                continue;
                            }
                        }
                        if(top.p==3){
                            if(top.y<n-1&&board[top.x][top.y+1]==word[wind]){
                                var node={
                                    c:word[wind],
                                    x:top.x,
                                    y:top.y+1,
                                    p:0
                                };
                                stack.push(node);
                                board[node.x][node.y]='*';
                                wind++;
                                if(wind==word.length){
                                    return true;
                                }
                                continue;
                            }
                        }
                        board[top.x][top.y]=top.c;
                        stack.pop();
                        wind--;
                    }
                }
            }
        }
        
        return false;
    };
  • 相关阅读:
    loadrunner安装问题
    (转)经典SQL练习题
    mysql存储过程-汇总学习
    MongoDB 添加用户名和密码
    解决端口占用,查看并杀掉端口
    在 Nest.js 中使用 MongoDB 与 TypeORM
    安装 mysqlclient 报 mysql_config not found
    修改 div 的滚动条的样式
    Ubuntu 系统连接到服务器
    Python 编程入门(4):变量与赋值
  • 原文地址:https://www.cnblogs.com/shytong/p/5096573.html
Copyright © 2011-2022 走看看