-
Notifications
You must be signed in to change notification settings - Fork 138
/
Solution.java
66 lines (58 loc) · 2.04 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
import java.io.*;
import java.util.*;
class Pair {
int p, c;
Pair (int f, int s) {
this.p = f;
this.c = s;
}
}
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] board = new int[100];
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
for (int i = 0; i < 100; i++) {
board[i] = Integer.MAX_VALUE;
}
HashMap<Integer, Integer> ladders = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> snakes = new HashMap<Integer, Integer>();
int N = sc.nextInt();
for (int n = 0; n < N; n++) {
int x = sc.nextInt();
int y = sc.nextInt();
ladders.put(x - 1, y - 1);
}
int M = sc.nextInt();
for (int n = 0; n < M; n++) {
int x = sc.nextInt();
int y = sc.nextInt();
snakes.put(x - 1, y - 1);
}
LinkedList<Pair> queue = new LinkedList<Pair>();
board[0] = 0;
queue.addFirst(new Pair(0, 0));
while (queue.size() > 0) {
Pair current = queue.pollLast();
if (current.c == board[current.p]) {
current.c++;
for (int i = 1; i <= 6 && current.p + i < 100; i++) {
int tmpP = current.p + i;
if (ladders.containsKey(tmpP)) { tmpP = ladders.get(tmpP); }
if (snakes.containsKey(tmpP)) { tmpP = snakes.get(tmpP); }
if (current.c < board[tmpP]) {
board[tmpP] = current.c;
queue.addFirst(new Pair(tmpP, current.c));
}
}
}
}
if (board[99] == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(board[99]);
}
}
}
}