Skip to content

Commit 95681e0

Browse files
authored
[20250825] BOJ / G5 / 뱀과 사다리 게임 / 이인희
1 parent 9e4c426 commit 95681e0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static BufferedReader br;
7+
public static void main(String[] args) throws IOException {
8+
br = new BufferedReader(new InputStreamReader(System.in));
9+
String[] temp = br.readLine().split(" ");
10+
int N = Integer.parseInt(temp[0]);
11+
int M = Integer.parseInt(temp[1]);
12+
13+
Map<Integer, Integer> move = new HashMap<>();
14+
for (int i = 0; i < N; i++) {
15+
temp = br.readLine().split(" ");
16+
move.put(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
17+
}
18+
for (int i = 0; i < M; i++) {
19+
temp = br.readLine().split(" ");
20+
move.put(Integer.parseInt(temp[0]), Integer.parseInt(temp[1]));
21+
}
22+
boolean[] visited = new boolean[101];
23+
Queue<int[]> q = new ArrayDeque<>();
24+
q.add(new int[]{1, 0}); //QElement: {현재칸, 주사위횟수}
25+
visited[1] = true;
26+
while (!q.isEmpty()) {
27+
//
28+
int[] cur = q.poll();
29+
int now = cur[0];
30+
int count = cur[1];
31+
if (now == 100) {
32+
System.out.println(count);
33+
return;
34+
}
35+
36+
for (int d = 1; d <= 6; d++) {
37+
int np = now + d;
38+
if (np > 100) continue;
39+
if (move.containsKey(np))
40+
np = move.get(np);
41+
if (!visited[np]) {
42+
visited[np] = true;
43+
q.add(new int[]{np, count + 1});
44+
}
45+
}
46+
}
47+
}
48+
}
49+
```

0 commit comments

Comments
 (0)