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 =
[
['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, 在矩阵中寻找字符串,回溯算法的更多相关文章
- LeetCode第[79]题(Java):Word Search(矩阵单词搜索)
题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...
- uva1330 在一个大的矩阵中寻找面积最大的子矩阵
大白书 P50页 #include <algorithm> #include <cstdio> using namespace std; ; int ma[maxn][maxn ...
- 在图中寻找最短路径-----深度优先算法C++实现
求从图中的任意一点(起点)到另一点(终点)的最短路径,最短距离: 图中有数字的点表示为图中的不同海拔的高地,不能通过:没有数字的点表示海拔为0,为平地可以通过: 这个是典型的求图中两点的最短路径:本例 ...
- 【剑指 Offer】12.矩阵中的路径
题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左.右.上.下移动一格. 如果一条路径经过了矩阵的某一格,那么 ...
- 矩阵中的路径 DFS+剪枝
给定一个 m x n 二维字符网格 board 和一个字符串单词 word .如果 word 存在于网格中,返回 true :否则,返回 false . 单词必须按照字母顺序,通过相邻的单元格内的字母 ...
- 【译】Java中的字符串字面量
原文地址:https://javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html 作者:Corey McGlone 让我们由一个简 ...
- linux内核驱动中对字符串的操作【转】
转自:http://www.360doc.com/content/12/1224/10/3478092_255969530.shtml Linux内核中关于字符串的相关操作,首先包含头文件: #inc ...
- 有关字符串的算法(KMP,Manacher,BM)陆续补充
KMP算法: 引言: KMP算法是一种改进的字符串匹配算法 字符串匹配:即寻找str_target在str_source中出现的位置 没有改进的字符串匹配:用暴力法进行搜索,枚举出所有的情况然后一一比 ...
- word search(二维数组中查找单词(匹配字符串))
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
随机推荐
- 巨蟒python全栈开发flask7 语音识别升级版&&mongoDB
1.web简陋版玩具 首先,复制上一节课的内容,将其中的语音文件删除掉, 放入三个文件,然后,我们需要在app写入下面的内容 下图是需要修改的地方: Recorder_ok.js是一个web录音的工具 ...
- PLSQL Developer在未安装Oracle Client情况下连接Oracle
常用的Oracle开发的工具有SQL Developer和PL/SQL Developer, 用PL/SQL连接oracle数据库,不管是本地的还是远程的,一般都需要安装oracle客户端 如何达到不 ...
- 用jq实现鼠标移入按钮背景渐变其他的背景效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Runtime Error! R6025-pure virtual function call 问题怎么解决
一.故障现象:1.360软件的木马查杀.漏洞修复等组件不能使用,提示runtime error2.暴风影音等很多软件不能正常使用3.设备管理器不能打开,提示“MMC 不能打开文件”4.部分https安 ...
- c++编译/连接/运行
1.gcc命令&makefile语法&makefile编写: https://www.cnblogs.com/ycloneal/p/5230266.html 2.头文件&库文件 ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- 在Web上运行Linux—js/linux模拟器
一个叫Fabrice Bellard 的程序员写了一段Javascript在Web浏览器中启动Linux(原网页,我把这个网页iframe在了下面),目前,你只能使用Firefox 4和Chrome ...
- 购物单问题—WPS使用excel
**** 180.90 88折 **** 10.25 65折 **** 56.14 9折 **** 104.65 ...
- 004-java类保存优化
设置路径:windows→preferences→java→editer→save Actions 1.设置如下 2.可单击右侧configure配置 2.1.code organorganizing ...
- React官网首页demo(单文件实现版)
本博客实现React官网首页上展示的demo, 为了方便直接采用单文件的形式, 如果想完整集成 在自己的项目中, 可以参考React官网的安装指南, 安装Create React App. hello ...