当前位置:网站首页>[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;
}
}
边栏推荐
- CORTEX-A系列处理器
- Count different types of data according to different times (stored procedures)
- 复杂网络-常用绘图软件和库
- CAN控制器的位同步过程
- 我为大厂怒刷的《100道Android面试题》
- 力扣 剑指 Offer II 094. 最少回文分割
- High voltage MOS tube knx42150 1500v/3a is applied to frequency converter power supply inverter, etc
- 力扣 729. 我的日程安排表 I
- 倍福PLC和C#通过ADS通信传输int数组类型变量
- 从List<Map>中截取指定的范围数据集合
猜你喜欢

In the Internet era, how to refine user operations?

Beifu PLC and C transmit bool array variables through ads communication

功能测试转自动化测试,十年自动化测试经验分享

Outlook tutorial, how to switch calendar views and create meetings in outlook?

第十天笔记

信號完整性(SI)電源完整性(PI)學習筆記(三十二)電源分配網路(四)

倍福PLC和C#通过ADS通信传输Bool数组变量

Uncaught (in promise) Neo4jError: WebSocket connection failure. Due to security constraints in your

OpenCV图像处理(下) 边缘检测+模板匹配+霍夫变换

Image processing image feature extraction and description
随机推荐
What is the reason for the failure of video playback and RTMP repeated streaming on easygbs platform?
Are there any academic requirements for career transfer software testing? Is there really no way out below junior college?
[ACTF2020 新生赛]BackupFile 1
Shooting games lesson 1-2: using sprites
UI自动化
射击 第 1-01 课:入门
Numpy:基本操作快速入门
Evaluation of classification model
Pod topology constraints
【离线语音专题④】安信可VC离线语音开发板二次开发语音控制LED灯
根据不同时间统计不同类型的数据(存储过程)
How to prevent repeated payment of orders?
软件测试岗位饱和了?自动化测试是新一代‘offer’技能
Opencv video operation
When using fastjson to parse and assign JSON data, the order of JSON fields is inconsistent
Opencv image processing (Part 2): edge detection + template matching + Hough transform
信号完整性(SI)电源完整性(PI)学习笔记(三十一)电源分配网路(三)
北汇信息12岁啦|Happy Birthday
Intégrité du signal (si) intégrité de l'alimentation électrique (PI) notes d'apprentissage (32) Réseau de distribution d'énergie (4)
The unity model is displayed in front of the UI, and the UI behind it jitters