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 ...
随机推荐
- iOS 程序切换后台
1. -(void)animationFinished:(NSString*)animationid finished:(NSNumber*)finished context:(void*)conte ...
- 延迟任务和循环任务ScheduledExecutorService
public class ScheduledThreadPool { public static ScheduledExecutorService scheduledThreadPool = Exec ...
- angualar入门学习-- 自定义指令 指令编译执行过程
3个阶段: 一.加载阶段 加载angular.js的源码,找到ng-app确定应用边界范围. 二.编译阶段 compile 查找所有指令,保存在一个列表中 对所有指令按优先级(property属性值) ...
- SPOJ Number of Palindromes(回文树)
Number of Palindromes Time Limit: 100MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu ...
- rainbow table 彩虹表
RainbowTable 的使用和性能的小测试 - SV的边界 - CSDN博客 https://blog.csdn.net/cecilulysess/article/details/4804707 ...
- Career Planning:Developers Best Practices Tutorial
This small tutorial is based on my past 16+ years of experience in software development industry. I ...
- SpringMVC 之 RESTful 风格的增删改查
1. 视图和视图解析器 视图解析器 请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View 或 ModelMap 等类型的处理方法, SpringMVC 也会 ...
- C++ 常见字符处理 收录
1.string字符串删除 字符串中 指定字符 std::string& HTTPRequestHandlerImpl::replace_all_distinct(std::string&am ...
- gdb各种调试命令和技巧
陈皓:用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在UNIX平台 ...
- C# 建立UDP服务器并接收客户端数据
C# 建立UDP服务器并接收客户端数据 2015-02-11 17:20 1218人阅读 评论(0) 收藏 举报 分类: C#开发技术(22) 版权声明:本文为博主原创文章,未经博主允许不得转载. ...