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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
| package com.example.demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
/**
*nowcoder : HJ43 宫问题
* https://www.nowcoder.com/profile/100401824/codeBookDetail?submissionId=426841626
* 迷宫问题:
* 5 5
* 0 1 0 0 0
* 0 1 1 1 0
* 0 0 0 0 0
* 0 1 1 1 0
* 0 0 0 1 0
*
*
* 5 5
* 0 1 0 0 0
* 0 1 0 1 0
* 0 0 0 0 1
* 0 1 1 1 0
* 0 0 0 0 0
*
*/
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Maze {
static int[] up = {-1, 0};
static int[] down = {1, 0};
static int[] left = {0, -1};
static int[] right = {0, 1};
static int[][] directions = new int[][]{
right, down, left, up,
};
// {0,0},{1,0},{2,0},{2,1},{2,2},{2,3} ,{2,4},{3,4},{4,4}
static List<int[]> pathList = new ArrayList<>();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String nm = in.nextLine();
int n = Integer.parseInt(nm.split("\\s+")[0]);
int m = Integer.parseInt(nm.split("\\s+")[1]);
int[][] matix = new int[n][m];
// boolean[][] visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
String line = in.nextLine();
String[] lines = line.split("\\s+");
for (int j = 0; j < lines.length; j++) {
matix[i][j] = Integer.parseInt(lines[j]);
}
}
System.out.println("DFS =====================================");
pathList.clear();
dfs(matix, 0, 0);
pathList.clear();
System.out.println("BFS=====================================");
List<int[]> paths = bfs(matix);
if (paths != null && paths.size() > 0) {
for (int[] p : paths) {
System.out.println("(" + p[0] + "," + p[1] + ") ");
}
}
System.out.println("dfsNotRecursion =====================================");
List<int[]> dfsNotRecursion = dfsNotRecursion(matix);
if (dfsNotRecursion != null && dfsNotRecursion.size() > 0) {
for (int[] p : dfsNotRecursion) {
System.out.println("(" + p[0] + "," + p[1] + ") ");
}
}
System.out.println(" =====================================");
}
}
public static void dfs(int[][] matix, int x, int y) {
//行数
int rows = matix.length;
// 列数
int cols = matix[0].length;
// 访问标识
int[][] visited = new int[rows][cols];
visited[x][y] = 2 ;
pathList.add(new int[]{x, y});
// 找到解了
if ((x == rows - 1 && y == cols - 1)) {
for (int[] arr : pathList) {
System.out.println("(" + arr[0] + "," + arr[1] + ") ");
}
return;
}
// 4个方向
for (int i = 0; i < directions.length; i++) {
int[] d = directions[i];
if (checkEdge(x + d[0], y + d[1], rows, cols)
&& matix[x + d[0]][y + d[1]] == 0
&& visited[x + d[0]][y + d[1]] == 0
) {
dfs(matix, x + d[0], y + d[1]);
// 重要 一定要有返回
return;
}
}
// 回溯
pathList.remove(pathList.size() - 1);
}
//深度搜索需要维护前驱节点和访问标识
public static List<int[]> bfs(int[][] matix) {
//队列,同时弹出节点的往多个方向搜索
Queue<int[]> queue = new LinkedList<>();
//跟踪记录前驱节点,用于回溯路径
Map<int[], int[]> traceMap = new HashMap<>();
// 记录访问的标识(后期优化可用于记录对应位置在路径上距离起点的距离)
int rows = matix.length;
int clos = matix[0].length;
int[][] visited = new int[rows][clos];
// 0,0 节点 入栈,标记为访问,并跟踪
int[] startNode = new int[]{0, 0, 1};
queue.offer(startNode);
traceMap.put(startNode, null);
visited[startNode[0]][startNode[1]] = startNode[2];
while (!queue.isEmpty()) {
int[] current = queue.poll();
// 已经到终点了
if (current[0] == rows - 1 && current[1] == clos - 1) {
List<int[]> paths = new ArrayList<>();
while (current != null) {
paths.add(current);
current = traceMap.get(current);
}
// 修正为从起点开始输出
Collections.reverse(paths);
// for(int[] v : visited){
// for(int p : v){
// System.out.print(p+ " ");
// }
// System.out.println("");
// }
return paths;
}
// 将当前节点的所有邻居节点压栈(本题不含斜线方向)
for (int[] d : directions) {
int[] nexts = new int[]{current[0] + d[0], current[1] + d[1], current[2] + 1};
if (
// 迷宫范围
checkEdge(nexts[0], nexts[1], rows, clos)
// 未被访问过,防止死循环
&& visited[nexts[0]][nexts[1]] == 0
// 不是墙
&& matix[nexts[0]][nexts[1]] == 0
) {
queue.offer(nexts);
traceMap.put(nexts, current);
visited[nexts[0]][nexts[1]] = nexts[2];
}
}
}
return Collections.emptyList();
}
public static List<int[]> dfsNotRecursion(int[][] matix) {
//行数
int rows = matix.length;
// 列数
int cols = matix[0].length;
// 访问标识
int[][] visited = new int[rows][cols];
visited[0][0] = 2;
int[] start = new int[]{0, 0};
Stack<int[]> stack = new Stack<>();
stack.push(start);
while (!stack.isEmpty()) {
// 重要 : 先不要弹出
int[] current = stack.peek();
List<int[]> paths = new ArrayList<>();
if (current[0] == rows - 1 && current[1] == cols - 1) {
// 直接一个个弹出打印
int[] pop;
while (!stack.isEmpty()) {
pop = stack.pop();
paths.add(pop);
}
Collections.reverse(paths);
return paths;
}
// 将当前节点的某个邻居节点压栈(本题不含斜线方向)
// 重要:addNew 无法添加则回退 标识
boolean addNew = false;
for (int[] d : directions) {
int[] nexts = new int[]{current[0] + d[0], current[1] + d[1]};
if (
// 迷宫范围
checkEdge(nexts[0], nexts[1], rows, cols)
// 不是墙
&& matix[nexts[0]][nexts[1]] == 0
// 未被访问过,防止死循环
&& visited[nexts[0]][nexts[1]] == 0
) {
stack.push(nexts);
visited[nexts[0]][nexts[1]] = 2;
addNew = true;
// 重要 :一次只能一个方向
break;
}
}
// 重要 : 无法添加则回退
if (!stack.isEmpty() && !addNew) {
stack.pop();
}
}
return Collections.emptyList();
}
public static boolean checkEdge(int x, int y, int rows, int cols) {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
}
|