问题描述:

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 =

[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

算法分析:回溯算法问题。遍历字母表矩阵,然后判断每个字母的上下左右方向是否匹配。维护一个visited数组,来记录路径上已经访问过的元素,防止重复访问。

public class WordSearch
{
private int row;
private int col;
public boolean exist(char[][] board, String word) {
row = board.length;
col = board[0].length;
boolean[][] visited = new boolean[row][col];
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (dfs(board, word, 0, i, j, visited))
return true;
}
}
return false;
} private boolean dfs(char[][] board, String word, int index, int rowindex,
int colindex, boolean[][] visited)
{
if (index == word.length())//word全部匹配,直接返回true
return true;
if (rowindex < 0 || colindex < 0 || rowindex >= row || colindex >= col)
return false;
if (visited[rowindex][colindex])//路径上一步访问过,就忽略
return false;
if (board[rowindex][colindex] != word.charAt(index))//不匹配,直接返回false
return false;
visited[rowindex][colindex] = true;//匹配,置visited为true
//当前元素的上下左右是否匹配,只要有一个方向匹配,返回true
boolean res = dfs(board, word, index + 1, rowindex - 1, colindex,
visited)
|| dfs(board, word, index + 1, rowindex + 1, colindex, visited)
|| dfs(board, word, index + 1, rowindex, colindex + 1, visited)
|| dfs(board, word, index + 1, rowindex, colindex - 1, visited);
visited[rowindex][colindex] = false;//递归完后将visited重置为false
return res;
}
}

Word Search, 在矩阵中寻找字符串,回溯算法的更多相关文章

  1. LeetCode第[79]题(Java):Word Search(矩阵单词搜索)

    题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...

  2. uva1330 在一个大的矩阵中寻找面积最大的子矩阵

    大白书 P50页 #include <algorithm> #include <cstdio> using namespace std; ; int ma[maxn][maxn ...

  3. 在图中寻找最短路径-----深度优先算法C++实现

    求从图中的任意一点(起点)到另一点(终点)的最短路径,最短距离: 图中有数字的点表示为图中的不同海拔的高地,不能通过:没有数字的点表示海拔为0,为平地可以通过: 这个是典型的求图中两点的最短路径:本例 ...

  4. 【剑指 Offer】12.矩阵中的路径

    题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左.右.上.下移动一格. 如果一条路径经过了矩阵的某一格,那么 ...

  5. 矩阵中的路径 DFS+剪枝

    给定一个 m x n 二维字符网格 board 和一个字符串单词 word .如果 word 存在于网格中,返回 true :否则,返回 false . 单词必须按照字母顺序,通过相邻的单元格内的字母 ...

  6. 【译】Java中的字符串字面量

    原文地址:https://javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html 作者:Corey McGlone 让我们由一个简 ...

  7. linux内核驱动中对字符串的操作【转】

    转自:http://www.360doc.com/content/12/1224/10/3478092_255969530.shtml Linux内核中关于字符串的相关操作,首先包含头文件: #inc ...

  8. 有关字符串的算法(KMP,Manacher,BM)陆续补充

    KMP算法: 引言: KMP算法是一种改进的字符串匹配算法 字符串匹配:即寻找str_target在str_source中出现的位置 没有改进的字符串匹配:用暴力法进行搜索,枚举出所有的情况然后一一比 ...

  9. word search(二维数组中查找单词(匹配字符串))

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

随机推荐

  1. Null value was assigned to a property of primitive type setter

    org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property of primitive t ...

  2. python3 简单爬虫

    爬取虎牙直播分类页面的主播的头像,名字,人气 今天学习了python3爬虫,上课闲着无聊,自己写了一个爬虫 就顺着老师思路 爬了虎牙直播分类页面的主播,头像,名字,和人气 HuYaCateScrapy ...

  3. python之设置小数保留位数

    python之设置小数保留位数 test.py: a = [3,4,4,4,6,4] average1 = float(sum(a)/len(a)) average2 =round(average1, ...

  4. 13.php面向对象

    1.构造函数 public __construct() {} 2.析构函数 public __destruct() {} 3.对象调用属性 //数以调用时候要用    -> 而不是Java的点. ...

  5. MyBatis 映射文件详解

    1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...

  6. 转!!xss漏洞

    参考资料 https://blog.csdn.net/jiangzhexi/article/details/56841793 http://www.freebuf.com/articles/web/4 ...

  7. window下安装mysql详细步骤

    1.下载安装包 打开mysql官网下载页面:http://dev.mysql.com/downloads/mysql/ 1.选择相应的版本和平台 2.mysql配置 打开刚刚解压的文件夹F:\mysq ...

  8. qt下通过socket传送中文

    zz 1.在main函数里我之前就加了一句QTextCodec::setCodecForTr( QTextCodec::codecForLocale() ); 现在再加一句QTextCodec::se ...

  9. QT Creator常用快捷键

    1.F10,F11 单步调试 2.Ctrl + / :注释/取消注释选定内容. 3.F4 :在 头文件(.h) 和 实现文件(.cpp) 之间进行切换. 4.Ctrl + i :自动格式化选中代码. ...

  10. 吴超老师课程---Hadoop的伪分布安装

    1.1 设置ip地址    执行命令    service network restart    验证:         ifconfig1.2 关闭防火墙    执行命令    service ip ...