题目:

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. URAL1991 The battle near the swamp 2017-04-12 18:07 92人阅读 评论(0) 收藏

    The battle near the swamp Gungan: Jar Jar, usen da booma!  Jar Jar: What? Mesa no have a booma!  Gun ...

  2. 轉:Jquery绑定img的click事件

    用JQUERY给IMG element绑定click事件的时候,直接用img.click(function(){...})不起作用,如下面代码$("img.ms-rteImage-Light ...

  3. 咏南CS开发框架新的界面风格

    咏南CS开发框架新的界面风格 这种风格完全和WINDOWS桌面一样,符合用户的操作习惯. 我的桌面的图标和WINDOWS桌面一样分为二类:1)快捷方式  2)文件夹. 如果快捷方式较多,看得人眼花缭乱 ...

  4. Xamarin 技术解析

    Xamarin 是一套基于C#语言的跨平台移动应用开发工具,今年2月份微软宣布收购Xamarin,而后在4月份进行的Build大会上微软宣布将会在各个版本的Visual Studio中免费提供Xama ...

  5. Flask测试和部署

    一 蓝图Blueprint 为什么学习蓝图? 我们学习Flask框架,是从写单个文件,执行hello world开始的.我们在这单个文件中可以定义路由.视图函数.定义模型等等.但这显然存在一个问题:随 ...

  6. 使用Intellij Idea连接Team Foundation Server (TFS)实现代码版本管理

    Intellij Idea是一个Java项目开发工具,支持Windows,MAC OS和Linux的跨平台开发环境,具备良好和智能的用户界面,在欧洲市场拥有很多粉丝.https://www.jetbr ...

  7. mysql免安装版初始化

    解压之后复制my-default.ini到本地目录下的my.ini 修改key: basedir = D:\\software\mysql-5.7.12-winx64  datadir = D:\\s ...

  8. 离线下载解决Nuget程序包及其依赖包的方法

    由于使用的一台电脑没有联网,但是需要asp.net core项目时使用到一个package,于是在nuget.org上手动下载.但是最后发现,依赖的包实在太多,手动下载太费时.于是晚上花时间研究了一下 ...

  9. 'Install app for SharePoint': Sideloading of apps is not enabled on this site

    http://blog.lekman.com/2012/11/sharepoint-2013-sideloading-of-apps-is.html Solution: You need to ena ...

  10. 2、Orcal数据库创建第一个(管理员)连接

    (注意这里第一个创建的是管理员连接也是我们的总连接,之后我们所有的其他新用户都要创建在它里面,所以它的一些属性我们在填写以及设置时需要注意!!!) 1.确认Orcal服务开启: 2.创建连接: 打开我 ...