-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeeAlgo.java
65 lines (57 loc) · 1.92 KB
/
LeeAlgo.java
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
import java.util.ArrayDeque;
import java.util.Deque;
/**
* Created by paulp on 10/11/2015.
*/
public class LeeAlgo { // bfs for grid
private int[][] paths;
private int width;
private int height;
public LeeAlgo(char[][] grid, int height, int width, Pair s) {
if (grid[s.x][s.y] == '-') return;
this.width = width;
this.height = height;
paths = new int[height][width];
paths[s.x][s.y] = 1;
Deque<Pair> queue = new ArrayDeque<>();
queue.add(s);
while (!queue.isEmpty()) {
Pair v = queue.poll();
for (int i = v.x - 1; i <= v.x + 1; ++i) {
for (int j = v.y - 1; j <= v.y + 1; ++j) {
if (i < 0 || j < 0 || i >= height || j >= width || (i == v.x && j == v.y) || grid[i][j] == '-' || paths[i][j] > 0) {
continue;
}
paths[i][j] = paths[v.x][v.y] + 1;
queue.add(new Pair(i, j));
}
}
}
}
public Iterable<Pair> pathTo(Pair p) {
assert hasPathTo(p);
int x = p.x;
int y = p.y;
int state = paths[x][y];
Deque<Pair> stack = new ArrayDeque<>();
stack.push(new Pair(x, y));
while (state != 1) {
brk : for (int i = x - 1; i <= x + 1; ++i) {
for (int j = y - 1; j <= y + 1; ++j) {
if (i < 0 || j < 0 || i >= height || j >= width || (i == x && j == y) || (paths[i][j] == 0) || paths[i][j] >= state) {
continue;
}
x = i;
y = j;
state = paths[i][j];
stack.push(new Pair(x, y));
break brk;
}
}
}
return stack;
}
public boolean hasPathTo(Pair p) {
return paths != null && paths[p.x][p.y] != 0;
}
}