GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — algorithm/src/offer/StringPathInMatrix12.java at main · duktig666/algorithm

Expand file tree

/

StringPathInMatrix12.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
224 lines (209 loc) · 8.59 KB
/

StringPathInMatrix12.java

Copy path

File metadata and controls

224 lines (209 loc) · 8.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package offer;
/**
* description:剑指Offer第12题——矩阵中的路径 @see https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/
*
* @author RenShiWei
* Date: 2021/9/9 9:33
**/
public class StringPathInMatrix12 {
// ------------------剑指offer题解----------------------
/**
* 判断矩阵中是否存在存在一条包含str所有字符的路径
*
* @param matrix 矩阵
* @param rows 矩阵的行数
* @param cols 矩阵的列数
* @param str 待判断的字符串路径
* @return 是否存在str的路径
*/
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if (matrix == null || rows < 1 || cols < 1 || str == null) {
return false;
}
// 定义布尔值矩阵(虽然是一个布尔值数组)
boolean[] visited = new boolean[rows * cols];
// 定义走到字符串的第几个字符
int pathLength = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 选取一个字符为起点,判断是否存在判定路径
if (hasPathCore(matrix, rows, cols, i, j, str, pathLength, visited)) {
return true;
}
}
}
return false;
}
/**
* 判断指定路经是否存在
*
* @param matrix 矩阵
* @param rows 矩阵的行数
* @param cols 矩阵的列数
* @param row 当前元素行
* @param col 当前元素列
* @param str 待判断的字符串路径
* @param pathLength 走到字符串的第几个字符
* @param visited 布尔值矩阵
* @return /
*/
public boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col, char[] str, int pathLength,
boolean[] visited) {
// 超出了字符串长度
if (pathLength > str.length - 1) {
return true;
}
// 定义是否存在路径
boolean hasPath = false;
// ①无越界 ②当前矩阵元素 = 当前第i个字符串路径的字符 ③当前路径未走过
if (row >= 0 && row < rows && col >= 0 && col < cols && matrix[row * cols + col] == str[pathLength] && ! visited[row * cols + col]) {
// 继续走
++ pathLength;
// 标记上一个已走过
visited[row * cols + col] = true;
// 递归下去(上下右左都看一遍)
hasPath = hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength, visited);
// 路径未走通
if (! hasPath) {
// 回溯
-- pathLength;
visited[row * cols + col] = false;
}
}
return hasPath;
}
public static void main(String[] args) {
char[] matrix = new char[] {'a', 'b', 'c', 'e', 's', 'f', 'c', 's', 'a', 'd', 'e', 'e'};
char[] str = new char[] {'b', 'f', 'c', 'e'};
char[] strFalse = new char[] {'a', 'b', 'f', 'b'};
StringPathInMatrix12 solution = new StringPathInMatrix12();
System.out.println(solution.hasPath(matrix, 3, 4, str));
System.out.println(solution.hasPath(matrix, 3, 4, strFalse));
char[][] matrix2 = new char[][] {{'a', 'b', 'c', 'e'}, {'s', 'f', 'c', 's'}, {'a', 'd', 'e', 'e'}};
String str1 = "bfce";
String str2 = "abfb";
System.out.println(solution.exist(matrix2, str1));
System.out.println(solution.exist(matrix2, str2));
}
// ------------------LeetCode题解 仿照上文----------------------
/**
* 判断指定路经是否存在
*
* @param board 矩阵
* @param word 路径字符串
* @return 是否存在路径
*/
public boolean exist(char[][] board, String word) {
if (board == null || board.length < 1 || board[0].length < 1 || word == null) {
return false;
}
int rows = board.length, cols = board[0].length;
char[] chars = word.toCharArray();
// 定义布尔值矩阵(虽然是一个布尔值数组)
boolean[][] visited = new boolean[rows][cols];
// 定义走到字符串的第几个字符
int pathLength = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 选取一个字符为起点,判断是否存在判定路径
if (existCore(board, rows, cols, i, j, chars, pathLength, visited)) {
return true;
}
}
}
return false;
}
/**
* 判断指定路经是否存在
*
* @param matrix 矩阵
* @param rows 矩阵的行数
* @param cols 矩阵的列数
* @param row 当前元素行
* @param col 当前元素列
* @param str 待判断的字符串路径
* @param pathLength 走到字符串的第几个字符
* @param visited 布尔值矩阵
* @return /
*/
public boolean existCore(char[][] matrix, int rows, int cols, int row, int col, char[] str, int pathLength,
boolean[][] visited) {
// 超出了字符串长度
if (pathLength > str.length - 1) {
return true;
}
// 定义是否存在路径
boolean hasPath = false;
// ①无越界 ②当前矩阵元素 = 当前第i个字符串路径的字符 ③当前路径未走过
if (row >= 0 && row < rows && col >= 0 && col < cols && matrix[row][col] == str[pathLength] && ! visited[row][col]) {
// 继续走
++ pathLength;
// 标记上一个已走过
visited[row][col] = true;
// 递归下去(上下右左都看一遍)
hasPath = existCore(matrix, rows, cols, row - 1, col, str, pathLength, visited)
|| existCore(matrix, rows, cols, row + 1, col, str, pathLength, visited)
|| existCore(matrix, rows, cols, row, col + 1, str, pathLength, visited)
|| existCore(matrix, rows, cols, row, col - 1, str, pathLength, visited);
// 路径未走通
if (! hasPath) {
// 回溯
-- pathLength;
visited[row][col] = false;
}
}
return hasPath;
}
// ------------------LeetCode题解 代码优化----------------------
/**
* 判断指定路经是否存在
*
* @param board 矩阵
* @param word 路径字符串
* @return 是否存在路径
*/
public boolean exist2(char[][] board, String word) {
char[] words = word.toCharArray();
// 依次从每个节点进行深搜,判断是否可得到路径
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (dfs(board, words, i, j, 0)) {
return true;
}
}
}
return false;
}
/**
* @param board 矩阵
* @param word 字符串路径
* @param i 矩阵行索引
* @param j 矩阵列索引
* @param k 当前目标字符在word中的索引
* @return 是否存在路径
*/
boolean dfs(char[][] board, char[] word, int i, int j, int k) {
//递归终止条件
// 边界值判断 数组元素不等于第k个目标字符,直接剪枝
if (i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k]) {
return false;
}
// 最后一个字符也相等,说明存在这样的一条路径
if (k == word.length - 1) {
return true;
}
// 当前矩阵元素标记为空字符,代表已经访问过(防止重复访问)
board[i][j] = '\0';
// 上下左右四个方向开启下层递归,只要有一个能找到,代表路径存在
boolean res = dfs(board, word, i + 1, j, k + 1)
|| dfs(board, word, i - 1, j, k + 1)
|| dfs(board, word, i, j + 1, k + 1)
|| dfs(board, word, i, j - 1, k + 1);
// 将当前元素再回复至初始值(只有相等时才会向下走,在这里时,之前的代码 board[i][j] = word[k] 是必然成立)
board[i][j] = word[k];
return res;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.