[BOJ] 17071 숨바꼭질 5

Time Lapse :36min 53sec

17071.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
#include <cstdio>
#include <queue>
using namespace std;
#define MAX 500000
int N, K, t;
int BFS(){
bool v[MAX+1][2]={false,};
queue<int> q;
q.emplace(N);
v[N][0] = true;
for(t=1;K+t<=MAX;++t){
K+=t;
int size = q.size();
for(int i = 0; i < size; ++i){
int n = q.front();
q.pop();
if(n&&!v[n-1][t&1]) v[n-1][t&1] = true, q.emplace(n-1);
if(n!=MAX&&!v[n+1][t&1]) v[n+1][t&1] = true, q.emplace(n+1);
if(n*2<=MAX&&!v[n*2][t&1]) v[n*2][t&1] = true, q.emplace(n*2);
}
if(v[K][t&1]) return t;
}
return -1;
}
int main(){
scanf("%d %d",&N, &K);
printf("%d",N==K?0:BFS());
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/17071/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.