Update 12. 矩阵中的路径.md

题目链接改为力扣中的对应题目,题目描述和代码按该题的接口调整。
This commit is contained in:
CyC2018
2026-10-28 13:45:03 +08:00
parent 229940fe35
commit 1c63f4cd05
+30 -54
View File
@@ -1,6 +1,6 @@
# 12. 矩阵中的路径
[牛客网](https://www.nowcoder.com/practice/69fe7a584f0a445da1b6652978de5c38?tpId=13&tqId=11218&tab=answerKey&from=cyc_github)
[Leetcode](https://leetcode.cn/problems/ju-zhen-zhong-de-lu-jing-lcof/)
## 题目描述
@@ -16,63 +16,39 @@
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/dc964b86-7a08-4bde-a3d9-e6ddceb29f98.png" width="200px"> </div><br>
本题的输入是数组而不是矩阵(二维数组),因此需要先将数组转换成矩阵。
```java
public class Solution {
private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
private int rows;
private int cols;
private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
private int rows;
private int cols;
public boolean hasPath (String val, int rows, int cols, String path) {
if (rows == 0 || cols == 0) return false;
this.rows = rows;
this.cols = cols;
char[] array = val.toCharArray();
char[][] matrix = buildMatrix(array);
char[] pathList = path.toCharArray();
boolean[][] marked = new boolean[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (backtracking(matrix, pathList, marked, 0, i, j))
return true;
return false;
}
private boolean backtracking(char[][] matrix, char[] pathList,
boolean[][] marked, int pathLen, int r, int c) {
if (pathLen == pathList.length) return true;
if (r < 0 || r >= rows || c < 0 || c >= cols
|| matrix[r][c] != pathList[pathLen] || marked[r][c]) {
return false;
}
marked[r][c] = true;
for (int[] n : next)
if (backtracking(matrix, pathList, marked, pathLen + 1, r + n[0], c + n[1]))
public boolean wordPuzzle(char[][] grid, String target) {
if (grid == null || grid.length == 0 || grid[0].length == 0) return false;
rows = grid.length;
cols = grid[0].length;
boolean[][] marked = new boolean[rows][cols];
char[] words = target.toCharArray();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (backtracking(grid, words, marked, 0, i, j))
return true;
marked[r][c] = false;
}
}
return false;
}
private boolean backtracking(char[][] grid, char[] words,
boolean[][] marked, int pathLen, int r, int c) {
if (pathLen == words.length) return true;
if (r < 0 || r >= rows || c < 0 || c >= cols
|| grid[r][c] != words[pathLen] || marked[r][c]) {
return false;
}
private char[][] buildMatrix(char[] array) {
char[][] matrix = new char[rows][cols];
for (int r = 0, idx = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
matrix[r][c] = array[idx++];
return matrix;
}
public static void main(String[] args) {
Solution solution = new Solution();
String val = "ABCESFCSADEE";
int rows = 3;
int cols = 4;
String path = "ABCCED";
boolean res = solution.hasPath(val, rows, cols, path);
System.out.println(res);
}
if (pathLen == words.length - 1) return true;
marked[r][c] = true;
for (int[] n : next)
if (backtracking(grid, words, marked, pathLen + 1, r + n[0], c + n[1]))
return true;
marked[r][c] = false;
return false;
}
```