-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon16928.cpp
More file actions
55 lines (45 loc) · 879 Bytes
/
baekjoon16928.cpp
File metadata and controls
55 lines (45 loc) · 879 Bytes
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
#include <iostream>
#include <queue>
using namespace std;
int N, M, x, y;
int moving[101], record[101];
void BFS(int start) {
queue < pair<int, int>> q;
q.push(make_pair(start, 0));
record[start] = 0;
while (!q.empty()) {
int now = q.front().first;
int count = q.front().second;
q.pop();
for (int i = 1; i <= 6; i++) {
int tempCount = count;
if (now + i > 100)
continue;
int next = now + i;
tempCount++;
if (moving[next] != 0) {
next = moving[next];
}
if (record[next] > tempCount) {
record[next] = tempCount;
q.push(make_pair(next, tempCount));
}
}
}
}
int main() {
cin >> N >> M;
for (int i = 1; i <= 100; i++) {
record[i] = 987654321;
}
for (int i = 0; i < N; i++) {
cin >> x >> y;
moving[x] = y;
}
for (int i = 0; i < M; i++) {
cin >> x >> y;
moving[x] = y;
}
BFS(1);
cout << record[100];
}