当前位置:网站首页>[jzof] path in matrix 12
[jzof] path in matrix 12
2022-07-23 13:19:00 【Sighed, angry】

Typical DFS The problem of the algorithm :
It is divided into four directions: up, down, left and right , To search .
Mark the search location before searching
import java.util.*;
public class Solution {
public boolean hasPath(char[][] matrix, String word) {
char[] words = word.toCharArray();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
// from [i,j] This coordinate starts to look up
if (dfs(matrix, words, i, j, 0))
return true;
}
}
return false;
}
boolean dfs(char[][] matrix, char[] word, int i, int j, int index) {
// The judgment of the boundary , If you cross the border and go back false.index It means to find a string word The first character of ,
// If this character doesn't equal matrix[i][j], It shows that verifying this coordinate path is impossible , Go straight back to false
if (i >= matrix.length || i < 0 || j >= matrix[0].length || j < 0 || matrix[i][j] != word[index])
return false;
// If word We've looked up every character of , Go straight back to true
if (index == word.length - 1)
return true;
// Save the value of the current coordinate , In order to recover in the end
char tmp = matrix[i][j];
// Then modify the value of the current coordinate
matrix[i][j] = '.';
// Go recursion , Up, down, left, right along the current coordinates 4 Look in two directions
boolean res = dfs(matrix, word, i + 1, j, index + 1)
|| dfs(matrix, word, i - 1, j, index + 1)
|| dfs(matrix, word, i, j + 1, index + 1)
|| dfs(matrix, word, i, j - 1, index + 1);
// Recursion and then restore the current coordinates
matrix[i][j] = tmp;
return res;
}
}
边栏推荐
- 从List<Map>中截取指定的范围数据集合
- 第十一天笔记
- Redis distributed lock practice
- 北大博士小姐姐:分享压箱底干货 | 五招提高学习效率
- Beifu PLC and C transmit int type variables through ads communication
- High voltage MOS tube knx42150 1500v/3a is applied to frequency converter power supply inverter, etc
- PostgreSQL k8s deployment template
- Record a reptile question bank
- 信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)
- Talk about study and work -- Qian Xuesen
猜你喜欢

第八天笔记

php框架MVC类代码审计

给1万帧视频做目标分割,显存占用还不到1.4GB | ECCV2022

Beihui information is 12 years old | happy birthday

倍福PLC和C#通过ADS通信定时刷新IO

如何防止订单重复支付?

机器学习:李航-统计学习方法(二)感知机+代码实现(原始+对偶形式)

设计思维的“布道者”

Image processing image feature extraction and description

Beifu PLC and C # regularly refresh IO through ads communication
随机推荐
从List<Map>中截取指定的范围数据集合
Beifu PLC and C transmit string array type variables through ads communication
Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)
将集合使用流进行分页
转行软件测试有学历要求吗?低于大专是真的没出路吗?
如何防止订单重复支付?
PostgreSQL k8s deployment template
信号完整性(SI)电源完整性(PI)学习笔记(三十二)电源分配网路(四)
Jupyter notebook添加已存在的虚拟环境
射击 第 1-01 课:入门
互联网时代下,如何精细化用户运营?
Ronge answer script production (latest in 2020)
记录一次爬虫题库
ZABBIX monitoring detailed installation to deployment
Convert the specified seconds to minutes and seconds
Beihui information is 12 years old | happy birthday
Opencv image processing (Part 2): edge detection + template matching + Hough transform
国信证券软件开户是安全吗?信息会不会泄露?
倍福PLC和C#通过ADS通信传输String数组类型变量
射击 第 1-3 课:图像精灵