본문 바로가기
Algorithm 💫/Problem Solving

[백준] 13549번: 숨바꼭질3 / C++

by 돼지고기맛있다 2021. 9. 17.
반응형

✏️ 문제 링크

https://www.acmicpc.net/problem/13549

 

13549번: 숨바꼭질 3

수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일

www.acmicpc.net

 

 

✏️ 문제 설명 (더보기 클릭 👆🏻)

 

✏️ 문제 코드

#include <bits/stdc++.h>
using namespace std;
using pii=pair<int, int>;
#define MAX 100001

vector<bool> vis(MAX, false);
int N, K;
int bfs(int s){
    queue<pii> q; q.push({s, 0});
    vis[s]=true;
    int answer=MAX;
    while(!q.empty()){
        pii t=q.front(); q.pop();
        int pos=t.first; int depth=t.second;
        
        vis[pos]=true;
        if(pos==K){
            answer=min(answer, depth);
            continue;
        }
        if(pos-1>=0 and !vis[pos-1]) q.push({pos-1, depth+1});
        if(pos+1<MAX and !vis[pos+1]) q.push({pos+1, depth+1});
        if(pos*2 <MAX and !vis[pos*2])q.push({pos*2, depth});
    }
    
    return answer;
}
int main(){
    
    cin>>N>>K;
    
    cout<<bfs(N);
 return 0;   
}

 

 

 

 ⭐ if feedback and question : comment please⭐  

 

 

반응형

댓글