-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPath.java
More file actions
176 lines (157 loc) · 5.38 KB
/
Copy pathShortestPath.java
File metadata and controls
176 lines (157 loc) · 5.38 KB
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
package bfs;
import org.junit.Test;
import java.util.LinkedList;
class Point {
int x;
int y;
Point() {
x = 0;
y = 0;
}
Point(int a, int b) {
x = a;
y = b;
}
}
public class ShortestPath {
/**
* @param grid: a chessboard included 0 (false) and 1 (true)
* @param source: a point
* @param destination: a point
* @return: the shortest path
* <p>
* 611. 骑士的最短路线
* 给定骑士在棋盘上的 初始 位置(一个2进制矩阵 0 表示空 1 表示有障碍物),找到到达 终点 的最短路线,返回路线的长度。如果骑士不能到达则返回 -1 。
* <p>
* 样例
* [[0,0,0],
* [0,0,0],
* [0,0,0]]
* source = [2, 0] destination = [2, 2] return 2
* <p>
* [[0,1,0],
* [0,0,0],
* [0,0,0]]
* source = [2, 0] destination = [2, 2] return 6
* <p>
* [[0,1,0],
* [0,0,1],
* [0,0,0]]
* source = [2, 0] destination = [2, 2] return -1
* 说明
* 如果骑士的位置为 (x,y),他下一步可以到达以下这些位置:
* <p>
* (x + 1, y + 2)
* (x + 1, y - 2)
* (x - 1, y + 2)
* (x - 1, y - 2)
* (x + 2, y + 1)
* (x + 2, y - 1)
* (x - 2, y + 1)
* (x - 2, y - 1)
* 注意事项
* 起点跟终点必定为空.
* 骑士不能穿过障碍物.
*/
public int shortestPath(boolean[][] grid, Point source, Point destination) {
// write your code here
grid[source.x][source.y] = true;
int result = 0;
LinkedList<Point> currentLevelSource = new LinkedList<>();
LinkedList<Point> nextLevelSource = new LinkedList<>();
if (source.x == destination.x && source.y == destination.y) {
return 0;
} else {
currentLevelSource.add(source);
return bfs(result, grid, currentLevelSource, nextLevelSource, destination);
}
}
private int bfs(int result, boolean[][] grid, LinkedList<Point> currentLevelSource,
LinkedList<Point>
nextLevelSource, Point destination) {
int grid_y = grid[0].length;
int grid_x = grid.length;
result++;
if (currentLevelSource.size() == 0) {
return -1;
}
while (currentLevelSource.size() != 0) {
Point point = currentLevelSource.poll();
int x = point.x;
int y = point.y;
if (x == destination.x && y == destination.y) {
// 因为遍历的是上一层的位置,所以返回上一层的result
return result - 1;
}
if (x + 2 < grid_x && y + 1 < grid_y && grid[x + 2][y + 1] != true) {
grid[x + 2][y + 1] = true;
nextLevelSource.add(new Point(x + 2, y + 1));
}
if (x + 2 < grid_x && y - 1 >= 0 && grid[x + 2][y - 1] != true) {
grid[x + 2][y - 1] = true;
nextLevelSource.add(new Point(x + 2, y - 1));
}
if (x - 2 >= 0 && y + 1 < grid_y && grid[x - 2][y + 1] != true) {
grid[x - 2][y + 1] = true;
nextLevelSource.add(new Point(x - 2, y + 1));
}
if (x - 2 >= 0 && y - 1 >= 0 && grid[x - 2][y - 1] != true) {
grid[x - 2][y - 1] = true;
nextLevelSource.add(new Point(x - 2, y - 1));
}
if (x + 1 < grid_x && y + 2 < grid_y && grid[x + 1][y + 2] != true) {
grid[x + 1][y + 2] = true;
nextLevelSource.add(new Point(x + 1, y + 2));
}
if (x + 1 < grid_x && y - 2 >= 0 && grid[x + 1][y - 2] != true) {
grid[x + 1][y - 2] = true;
nextLevelSource.add(new Point(x + 1, y - 2));
}
if (x - 1 >= 0 && y + 2 < grid_y && grid[x - 1][y + 2] != true) {
grid[x - 1][y + 2] = true;
nextLevelSource.add(new Point(x - 1, y + 2));
}
if (x - 1 >= 0 && y - 2 >= 0 && grid[x - 1][y - 2] != true) {
grid[x - 1][y - 2] = true;
nextLevelSource.add(new Point(x - 1, y - 2));
}
}
return bfs(result, grid, nextLevelSource, currentLevelSource, destination);
}
@Test
public void testShortestPath() {
int n = 6;
int m = 8;
boolean[][] grid = new boolean[m][n];
Point source = new Point(2, 0);
Point destination = new Point(2, 2);
// [[0,0,0,0,1,1],[1,0,1,0,0,1],[0,0,1,0,0,1],[0,0,1,1,0,1],
// [1,0,1,0,0,1],[0,0,1,0,0,1],[0,0,1,0,0,1],[0,0,1,0,0,1]]
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
grid[i][j] = false;
}
}
grid[0][4] = true;
grid[0][5] = true;
grid[1][0] = true;
grid[1][2] = true;
grid[1][5] = true;
grid[2][2] = true;
grid[2][5] = true;
grid[3][2] = true;
grid[3][3] = true;
grid[3][5] = true;
grid[4][0] = true;
grid[4][2] = true;
grid[4][5] = true;
grid[5][2] = true;
grid[5][5] = true;
grid[6][2] = true;
grid[6][5] = true;
grid[7][2] = true;
grid[7][5] = true;
int result = shortestPath(grid, source, destination);
System.out.println(result);
}
}