-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
72 lines (57 loc) · 2.27 KB
/
Solution.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
66
67
68
69
70
71
72
import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
/**
* @author Oleg Cherednik
* @since 31.01.2019
*/
public class Solution {
public static void main(String... args) {
boolean[][] board = {
{false, false, false, false},
{true, true, false, true},
{false, false, false, false},
{false, false, false, false}};
System.out.println(findMinimumPathLength(board, new Point(0, 3), new Point(0, 0)));
}
public static Integer findMinimumPathLength(boolean[][] board, Point from, Point to) {
int[][] steps = new int[board.length][board[0].length];
Arrays.stream(steps).forEach(row -> Arrays.fill(row, -1));
Deque<Point> queue = new LinkedList<>();
queue.add(from);
steps[from.y][from.x] = 0;
while (!queue.isEmpty()) {
Point point = queue.remove();
if (point.isEqual(to))
continue;
for (Point nextPoint : Arrays.asList(new Point(point.x + 1, point.y), new Point(point.x, point.y - 1),
new Point(point.x - 1, point.y), new Point(point.x, point.y + 1))) {
if (!isStepAvailable(board, nextPoint))
continue;
if (steps[nextPoint.y][nextPoint.x] >= 0 && steps[point.y][point.x] + 1 >= steps[nextPoint.y][nextPoint.x])
continue;
steps[nextPoint.y][nextPoint.x] = steps[point.y][point.x] + 1;
queue.add(nextPoint);
}
}
return steps[to.y][to.x] == Integer.MAX_VALUE ? null : steps[to.y][to.x];
}
private static boolean isStepAvailable(boolean[][] board, Point point) {
return point.y >= 0 && point.y < board.length && point.x >= 0 && point.x < board[point.y].length && !board[point.y][point.x];
}
private static final class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean isEqual(Point point) {
return x == point.x && y == point.y;
}
@Override
public String toString() {
return "[" + x + ';' + y + ']';
}
}
}