|
1 | 1 | # 12. 矩阵中的路径 |
2 | 2 |
|
3 | | -[NowCoder](https://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&tqId=11218&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github) |
| 3 | +[牛客网](https://www.nowcoder.com/practice/69fe7a584f0a445da1b6652978de5c38?tpId=13&tqId=11218&tab=answerKey&from=cyc_github) |
4 | 4 |
|
5 | 5 | ## 题目描述 |
6 | 6 |
|
|
19 | 19 | 本题的输入是数组而不是矩阵(二维数组),因此需要先将数组转换成矩阵。 |
20 | 20 |
|
21 | 21 | ```java |
22 | | -private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; |
23 | | -private int rows; |
24 | | -private int cols; |
25 | | - |
26 | | -public boolean hasPath(char[] array, int rows, int cols, char[] str) { |
27 | | - if (rows == 0 || cols == 0) return false; |
28 | | - this.rows = rows; |
29 | | - this.cols = cols; |
30 | | - boolean[][] marked = new boolean[rows][cols]; |
31 | | - char[][] matrix = buildMatrix(array); |
32 | | - for (int i = 0; i < rows; i++) |
33 | | - for (int j = 0; j < cols; j++) |
34 | | - if (backtracking(matrix, str, marked, 0, i, j)) |
35 | | - return true; |
| 22 | +public class Solution { |
| 23 | + private final static int[][] next = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; |
| 24 | + private int rows; |
| 25 | + private int cols; |
| 26 | + |
| 27 | + public boolean hasPath (String val, int rows, int cols, String path) { |
| 28 | + if (rows == 0 || cols == 0) return false; |
| 29 | + this.rows = rows; |
| 30 | + this.cols = cols; |
| 31 | + char[] array = val.toCharArray(); |
| 32 | + char[][] matrix = buildMatrix(array); |
| 33 | + char[] pathList = path.toCharArray(); |
| 34 | + boolean[][] marked = new boolean[rows][cols]; |
| 35 | + for (int i = 0; i < rows; i++) |
| 36 | + for (int j = 0; j < cols; j++) |
| 37 | + if (backtracking(matrix, pathList, marked, 0, i, j)) |
| 38 | + return true; |
36 | 39 |
|
37 | | - return false; |
38 | | -} |
| 40 | + return false; |
| 41 | + } |
39 | 42 |
|
40 | | -private boolean backtracking(char[][] matrix, char[] str, |
41 | | - boolean[][] marked, int pathLen, int r, int c) { |
| 43 | + private boolean backtracking(char[][] matrix, char[] pathList, |
| 44 | + boolean[][] marked, int pathLen, int r, int c) { |
42 | 45 |
|
43 | | - if (pathLen == str.length) return true; |
44 | | - if (r < 0 || r >= rows || c < 0 || c >= cols |
45 | | - || matrix[r][c] != str[pathLen] || marked[r][c]) { |
| 46 | + if (pathLen == pathList.length) return true; |
| 47 | + if (r < 0 || r >= rows || c < 0 || c >= cols |
| 48 | + || matrix[r][c] != pathList[pathLen] || marked[r][c]) { |
46 | 49 |
|
| 50 | + return false; |
| 51 | + } |
| 52 | + marked[r][c] = true; |
| 53 | + for (int[] n : next) |
| 54 | + if (backtracking(matrix, pathList, marked, pathLen + 1, r + n[0], c + n[1])) |
| 55 | + return true; |
| 56 | + marked[r][c] = false; |
47 | 57 | return false; |
48 | 58 | } |
49 | | - marked[r][c] = true; |
50 | | - for (int[] n : next) |
51 | | - if (backtracking(matrix, str, marked, pathLen + 1, r + n[0], c + n[1])) |
52 | | - return true; |
53 | | - marked[r][c] = false; |
54 | | - return false; |
55 | | -} |
56 | 59 |
|
57 | | -private char[][] buildMatrix(char[] array) { |
58 | | - char[][] matrix = new char[rows][cols]; |
59 | | - for (int r = 0, idx = 0; r < rows; r++) |
60 | | - for (int c = 0; c < cols; c++) |
61 | | - matrix[r][c] = array[idx++]; |
62 | | - return matrix; |
| 60 | + private char[][] buildMatrix(char[] array) { |
| 61 | + char[][] matrix = new char[rows][cols]; |
| 62 | + for (int r = 0, idx = 0; r < rows; r++) |
| 63 | + for (int c = 0; c < cols; c++) |
| 64 | + matrix[r][c] = array[idx++]; |
| 65 | + return matrix; |
| 66 | + } |
| 67 | + |
| 68 | + public static void main(String[] args) { |
| 69 | + Solution solution = new Solution(); |
| 70 | + String val = "ABCESFCSADEE"; |
| 71 | + int rows = 3; |
| 72 | + int cols = 4; |
| 73 | + String path = "ABCCED"; |
| 74 | + boolean res = solution.hasPath(val, rows, cols, path); |
| 75 | + System.out.println(res); |
| 76 | + } |
63 | 77 | } |
64 | 78 | ``` |
0 commit comments