백준
백준 소스코드 [C++] 17071 숨바꼭질 5
Hani_Levenshtein
2021. 4. 12. 19:11
17071번: 숨바꼭질 5
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 500,000)에 있고, 동생은 점 K(0 ≤ K ≤ 500,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일 때
www.acmicpc.net
백준 소스코드 [C++] 17071 숨바꼭질 5
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#include <cstdlib>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define pli pair<ll, int>
#define make_unique(v) sort(all(v)), v.erase(unique(all(v), v.end())
typedef long long ll;
using namespace std;
int n,m;
bool chk[500001][2];
int bfs(){
queue<int> q;
q.push(n);
chk[n][0]=true;
int time=1;
if(n==m) return 0;
while(!q.empty()){
int qSize = (int)q.size();
m+=time;
if(500000<m) return -1;
for(int size = 0; size<qSize;size++){
int here = q.front();
int there;
q.pop();
for(int i=0;i<3;i++){
if(i==0) there = here - 1;
else if(i==1) there = here + 1;
else there = here * 2;
if(0<=there && there<=500000 && !chk[there][time%2]){
chk[there][time%2]=true;
q.push(there);
}
}
}
if(chk[m][time%2]) return time;
time++;
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
cout<<bfs()<<'\n';
return 0;
}