-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day24PathFinder.java
62 lines (52 loc) · 1.91 KB
/
Day24PathFinder.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
import java.util.List;
import java.util.ArrayList;
public class Day24PathFinder{
private static int lcm(int a, int b){
return a*b / gcd(a,b);
}
private static int gcd(int a, int b){
return b==0 ? a : gcd(b, a%b);
}
private static Day24BlizzardField[] precomputeFields(Day24BlizzardField initState){
int maxSteps = lcm((initState.rows()-2),(initState.cols()-2));
Day24BlizzardField[] fields = new Day24BlizzardField[maxSteps];
fields[0] = initState;
System.out.println(fields[0].toString());
for(int k=1;k<maxSteps;k++){
fields[k] = fields[k-1].step();
System.out.println(fields[k].toString());
}
return fields;
}
private static List<Point> getNeighbors(Point p){
List<Point> neighbors = new ArrayList<Point>();
neighbors.add(new Point(p.getX()-1,p.getY()));
neighbors.add(new Point(p.getX()+1,p.getY()));
neighbors.add(new Point(p.getX(),p.getY()-1));
neighbors.add(new Point(p.getX(),p.getY()+1));
return neighbors;
}
public static int[][] solve(Day24BlizzardField initState, Point start, Point end){
int[][] distances = new int[initState.rows()][initState.cols()];
List<Point> points = new ArrayList<Point>();
for(int row=0;row<distances.length;row++){
for(int col=0;col<distances[0].length;col++){
if(row == 0 || row == (distances.length-1) || col == 0 || col == (distances[0].length-1)){
distances[row][col] = -1;
}else{
distances[row][col] = Integer.MAX_VALUE;
}
}
}
distances[start.getX()][start.getY()] = 0;
distances[end.getX()][end.getY()] = Integer.MAX_VALUE;
points.add(start);
Day24BlizzardField[] fields = precomputeFields(initState);
while(points.size() > 0){
System.out.println(points.toString());
Point p = points.remove(0);
System.out.println(p.toString() + " " + distances[p.getX()][p.getY()]);
}
return distances;
}
}