-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16928.cpp
67 lines (56 loc) · 974 Bytes
/
16928.cpp
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
#include <iostream>
#include <queue>
using namespace std;
int arr[101];
bool visited[101];
int N, M;
int a, b;
int roll[6] = { 1,2,3,4,5,6 };
struct node {
int now, cot;
};
void bfs()
{
queue<node> que;
que.push({ 1,0 });
visited[1] = true;
while (!que.empty())
{
int nnow = que.front().now;
int ncot = que.front().cot;
que.pop();
if (nnow == 100) {
cout << ncot;
return;
}
for (int i = 0; i < 6; i++)
{
int mnow = nnow + roll[i];
if (mnow >= 1 && mnow <= 100 && !visited[mnow]) {
if (arr[mnow])
{
visited[mnow] = true;
visited[arr[mnow]] = true;
mnow = arr[mnow];
}
else
{
visited[mnow] = true;
}
que.push({ mnow, ncot + 1 });
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> N >> M;
for (int x = 0; x < N + M; x++) {
cin >> a >> b;
arr[a] = b;
}
bfs();
return 0;
}