题目:

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

【数组】word search的更多相关文章

  1. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  3. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  4. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  5. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

  6. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  7. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...

  8. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  9. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

随机推荐

  1. (博弈 sg入门)kiki's game -- hdu -- 2147

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2147 题意: 在一个n*m的棋盘上,从  (1,m),即右上角开始向左下角走. 下棋者只能往左边(lef ...

  2. Ubuntu的常识使用了解2

    1. 在linux系统中,所有的目录(分区)都是挂靠在/跟目录下. 使用「cd」这个指令来切換目录.切换目录的方式:(1)绝对路径,以根目录做开头/, (2)相对目录, 没有以根目录开头. 注意:插补 ...

  3. Websocket出现的错误

    前端使用sockjs,后台使用spring的websocket框架 结果在一个网络较慢的地方,发现tomcat报错信息: Oct 28, 2015 10:10:43 AM org.apache.cat ...

  4. IDEA配置spring

    大半天都在看spring,以前总是看不下去,这次慢慢来,慢慢看. 看那些基础的,倒是还不错.好多都是关于helloworld的,写完helloworld,觉得不怎么形象.于是写了动物,作为接口. (1 ...

  5. 如何在CentOS 7中禁用IPv6

    最近,我的一位朋友问我该如何禁用IPv6.在搜索了一番之后,我找到了下面的方案.下面就是在我的CentOS 7 迷你服务器关闭IPv6的方法. 你可以用两个方法做到这个. 方法 1 编辑文件/etc/ ...

  6. xml与json的区别和总结

    JSON和XML的比较 ◆可读性 JSON和XML的可读性可谓不相上下,一边是简易的语法,一边是规范的标签形式,很难分出胜负. ◆可扩展性 XML天生有很好的扩展性,JSON当然也有,没有什么是XML ...

  7. pycharm中安装可以贴图片的Markdown插件

    方法一:(测试成功) 先安装官方推荐的Markdown support插件,再安装Paste images into MarkDown 如果Paste images into MarkDown插件在线 ...

  8. c# .NET RSA结合AES加密服务端和客户端请求数据

    这几天空闲时间就想研究一下加密,环境是web程序,通过js请求后台返回数据,我想做的事js在发送请求前将数据加密,服务端收到后解密,待服务端处理完请求后,将处理结果加密返回给客户端,客户端在解密,于是 ...

  9. nginx反向代理后abp的webapi host如何获取客户端ip?

    dotnet core 跨平台是微软伟大的创举,脱离iis后服务器成本都降低了. 问题 这不,采用abp搞了个小项目,部署到centos后发现审计日志里面的ip信息不对. 解决 这个问题在.net 4 ...

  10. docker-compose 部署 Redis

    信息: Docker版本($ docker --version):Docker版本18.06.1-ce,版本e68fc7a 系统信息($ cat /etc/centos-release):CentOS ...