-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12851_숨바꼭질2.cpp
62 lines (50 loc) · 1.11 KB
/
12851_숨바꼭질2.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
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#define MAX 100000 + 1
using namespace std;
int n, k;
int minSec = 0;
int cnt = 0;
bool visit[MAX];
int bfs(int n, int k)
{
queue<pair<int, int>> q;
q.push({n, 0});
visit[n] = true;
while (!q.empty())
{
int curLoc = q.front().first;
int curSec = q.front().second;
q.pop();
visit[curLoc] = true;
if (minSec && minSec == curSec && curLoc == k)
{
cnt++;
}
if (!minSec && curLoc == k)
{
minSec = curSec;
cnt++;
}
int plus = curLoc + 1;
int minus = curLoc - 1;
int mul = curLoc * 2;
if (plus < MAX && !visit[plus])
q.push({plus, curSec + 1});
if (minus >= 0 && !visit[minus])
q.push({minus, curSec + 1});
if (mul < MAX && !visit[mul])
q.push({mul, curSec + 1});
}
return minSec;
}
int main()
{
memset(visit, false, sizeof(visit));
cin >> n >> k;
cout << bfs(n, k) << endl;
cout << cnt << endl;
return 0;
}