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;
    };
  • 相关阅读:
    Centos或Windows中部署Zookeeper集群及其简单用法
    Linux中使用sendmail发送邮件,指定任意邮件发送人
    使用log4net将C#日志发送到Elasticsearch
    在Centos6或者7上安装Kafka最新版
    最简单的配置Centos中JAVA的环境变量的方法
    JAVA通过oshi获取系统和硬件信息
    JAVA代码中获取JVM信息
    使用JavaCV播放视频、摄像头、人脸识别
    JAVA中通过JavaCV实现跨平台视频/图像处理-调用摄像头
    Linux中使用Vim快速更换文档中Windows换行符为Linux平台
  • 原文地址:https://www.cnblogs.com/shytong/p/5096573.html
Copyright © 2011-2022 走看看