-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path16928.java
More file actions
60 lines (51 loc) · 1.8 KB
/
16928.java
File metadata and controls
60 lines (51 loc) · 1.8 KB
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class SnakeAndLadderGame {
static class Pos {
int pos;
int cnt;
public Pos(int pos, int cnt) {
this.pos = pos;
this.cnt = cnt;
}
}
public static void main(String[] args) throws IOException {
int N, M;
int[] ladder = new int[101];
int[] snake = new int[101];
boolean[] visited = new boolean[101];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
N = Integer.parseInt(input[0]);
M = Integer.parseInt(input[1]);
for(int i = 0; i < N; i++) {
input = br.readLine().split(" ");
ladder[Integer.parseInt(input[0])] = Integer.parseInt(input[1]);
}
for(int i = 0; i < M; i++) {
input = br.readLine().split(" ");
snake[Integer.parseInt(input[0])] = Integer.parseInt(input[1]);
}
br.close();
Queue<Pos> queue = new LinkedList<>();
queue.add(new Pos(1, 0));
while(!queue.isEmpty()) {
Pos cur = queue.poll();
visited[cur.pos] = true;
if(cur.pos == 100) {
System.out.println(cur.cnt);
break;
}
for(int num = 1; num <= 6; num++) {
int next = cur.pos + num;
if(next > 100) continue;
if(ladder[next] != 0) queue.add(new Pos(ladder[next], cur.cnt + 1));
else if(snake[next] != 0) queue.add(new Pos(snake[next], cur.cnt + 1));
else if(!visited[next]) queue.add(new Pos(next, cur.cnt + 1));
}
}
}
}