-
Notifications
You must be signed in to change notification settings - Fork 0
/
KingPath.java
117 lines (102 loc) · 3.04 KB
/
KingPath.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
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
/**
* https://codeforces.com/problemset/problem/242/C #dfs #hash-table #shortest-path
*
* <p>use Dijkstra with adjacency matrix other way: use HashMap, HashSet. override equals and
* hashCode of Point class (hashCode: x*p1 + y*p2 - max_val), p1, p2 is prime number and p1 # p2.
* weak point: take time to hashCode,.. not better than above way. (time limited at test case 23)
*/
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
public class KingPath {
static int max_val = (int) 1e9 + 1;
static boolean isValidPoint(TreeSet<Point> graph, Point point) {
if (point.x < 0 || point.x >= max_val) return false;
if (point.y < 0 || point.y >= max_val) return false;
if (!graph.contains(point)) return false;
return true;
}
static void Dijkstra(
TreeSet<Point> graph, TreeMap<Point, Integer> dist, Point source, Point destination) {
int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};
dist.put(source, 0);
Node startVertex = new Node(source, 0);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(startVertex);
while (!pq.isEmpty()) {
Node node = pq.poll();
Point point = node.p;
int w = node.dist;
if (dist.containsKey(point) && w > dist.get(point)) {
continue;
}
for (int i = 0; i < dx.length; i++) {
Point tmp = new Point(point.x + dx[i], point.y + dy[i]);
if (isValidPoint(graph, tmp)) {
if (dist.containsKey(tmp)) {
if (w + 1 < dist.get(tmp)) {
dist.replace(tmp, w + 1);
pq.add(new Node(tmp, dist.get(tmp)));
}
} else {
dist.put(tmp, w + 1);
pq.add(new Node(tmp, dist.get(tmp)));
}
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x0 = sc.nextInt();
int y0 = sc.nextInt();
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int n = sc.nextInt();
TreeSet<Point> graph = new TreeSet<>();
for (int i = 0; i < n; i++) {
int r = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
while (a <= b) {
graph.add(new Point(r, a++));
}
}
TreeMap<Point, Integer> dist = new TreeMap<>();
Point source = new Point(x0, y0);
Point destination = new Point(x1, y1);
Dijkstra(graph, dist, source, destination);
if (dist.containsKey(destination)) {
System.out.println(dist.get(destination));
} else {
System.out.println(-1);
}
}
}
class Point implements Comparable<Point> {
int x, y;
Point(int _x, int _y) {
this.x = _x;
this.y = _y;
}
public int compareTo(Point other) {
if (this.x == other.x) {
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
class Node implements Comparable<Node> {
Point p;
int dist;
Node(Point _p, int _dist) {
this.p = _p;
this.dist = _dist;
}
public int compareTo(Node other) {
return this.dist - other.dist;
}
}