【数组】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;
};
原文地址:https://www.cnblogs.com/shytong/p/5096573.html