题目:

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. Sophus VS2010编译不支持?C++11语法的缘故。那有没有不带C++11特性的Sophus版本呢?

    Eigen:3.1 3.0 Ceres:No Sophus: Sophus支不支持Windows编译?官网写的是通过了Windows的编译的 linux, os x:  windows:  code ...

  2. Linux 基础教程 29-tcpdump命令-1

    什么是tcpdump     在Linux中输入命令man tcpdump给出的定义如下所示: tcpdump - 转储网络上的数据流 是不是感觉很懵?我们用通俗.形象.学术的表达方式来全方位描述tc ...

  3. EBS常用接口表

    AP接口表: AP_INVOICES_INTERFACE AP_INVOICE_LINES_INTERFACE 涉及的请求: 应付款管理系统开放接口导入 涉及案例: 运费导AP.费用导AP PO接口表 ...

  4. CURL命令测试网站打开速度

    curl -o /dev/null -s -w %{time_namelookup}:%{time_connect}:%{time_starttransfer}:%{time_total} http: ...

  5. Spring中ApplicationContext和beanfactory区别---解析一

    BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是一种典型的BeanFactory.原始的BeanFactory无法支持spring的许多插件,如AOP ...

  6. SQL字符串分割转列,Sql列转字符串

    declare @strVar varchar(2000)declare @ResultVar varchar(2000)set @ResultVar='[薄饼],[点心],[海鲜],[酒吧],[楼面 ...

  7. JavaScript日期(参考资料)

    构造函数1.new Date() 如果没有参数,则Date的构造器会依据系统设置的当前时间来创建一个Date对象.2.new Date(value) value代表自1970年1月1日00:00:00 ...

  8. oracle数据导出导入(exp/imp)

    1.本地数据库导入导出 1.导出 (运行---cmd中操作)exp 用户名/密码@数据库实例名file=本地存放路径eg: exp jnjp/jnjp@ORCL file=C:/jnjp.dmp 2. ...

  9. 经典的兔子生兔子问题(C#递归解法)

    古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 思路:先求出每个月新增的兔子,再用循环求和即可算出这个月 ...

  10. 类似gitlab代码提交的热力图怎么做?

    本文由  网易云发布. 作者:张淞(本篇文章仅限知乎内部分享,如需转载,请取得作者同意授权.) 昨夜,网易有数产品经理路过开发的显示屏前见到了类型这样的一张图: 于是想到有数能不能做出这样的图来?作为 ...