【数组】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;
};
【数组】word search的更多相关文章
- [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 ...
- 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 ...
- Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- 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 ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
- [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 ...
- [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 ...
- leetcode 79. Word Search 、212. Word Search II
https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...
随机推荐
- Shell编程-04-Shell中变量数值计算
目录 算术运算符 算术运算命令 数值运算用法 算术运算符 在任何一门形式的语言中均会存在算术运算的情况,Shell常见的运算符如下所示: 运算符 含义 + - * / % 加 减 乘 除 求余 ...
- Struts+Spring+Hibernate整合
这段笔记三两年前写的,一直因为一些琐事,或者搞忘记了,没有发.今天偶然翻出了它,就和大家一起分享下吧. 1.导入jar包 Struts的jar包: Spring的jar包: Hibernate的jar ...
- flume学习以及ganglia(若是要监控hive日志,hive存放在/tmp/hadoop/hive.log里,只要运行过hive就会有)
python3.6hdfs的使用 https://blog.csdn.net/qq_29863961/article/details/80291654 https://pypi.org/ 官网直接搜 ...
- ASP.NET MVC验证DateTime的问题
今天碰到一个Bug,在IE8中,日期验证失效,输入正确的日期格式也会验证失败,提示:xxx必须是日期格式(the field xxx must be a date) 最终找到问题所在:jquery.v ...
- c++中的隐藏及重载、重写与隐藏的区别
c/c++中的隐藏 举个栗子 class A { public : void fun1(int a, int b) { cout<<"abcd"<<end ...
- .NET中异常与错误码优劣势对比
.NET之所以选择异常,而不是返回错误码来报告异常,是由于前者有以下几个优势: 1.异常与oop语言的结合性更好.oop语言经常需要对成员签名强加限制,比如c#中的构造函数.操作符重载和属性,开发者对 ...
- asp.net mvc 3 linq实现数据的增、删、改、查、
添加数据 定义一个对象: public class Student { public int id{get; set;} public string Name{get;set;} public str ...
- 再编写代码中报错:CS8107 C# 7.0 中不支持功能“xxxxxx”。请使用 7.1 或更高的语言版本。
解决方法:项目右键属性 ---> 生成 ---> 找到最下面的高级按钮,点击高级按钮 ---> 常规 ---> 语言版本 ---> 选择 C#最新次要版本,或者比当前版本 ...
- 「CodeChef - SKIRES」Ski Resort
题目链接 戳我 \(Description\) 给你一个\(n*m\)的网格,以及网格上的两个格子\(A,B\).每个格子有一个高度.每次操作可以选择一个格子(不能是\(A\)或\(B\))并将它的高 ...
- 【题解】 洛谷P2340 奶牛会展
传送门 重新开始打代码Day1 第一眼看感觉不对啊,这道题目好像空间开不下,是不是不能dp... 后来想到了一个思路,他要求的是\(dp_{i,j,k}=j+k\),然后这样子不是很奇怪吗? 直接一维 ...