반응형
✏️ 문제 링크
https://www.acmicpc.net/problem/13549
✏️ 문제 설명 (더보기 클릭 👆🏻)
✏️ 문제 코드
#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⭐
반응형
'Algorithm 💫 > Problem Solving' 카테고리의 다른 글
[백준] 12851번: 숨바꼭질2 / C++ (0) | 2021.09.17 |
---|---|
[백준] 14226번: 이모티콘 / C++ (0) | 2021.09.17 |
[백준] 17086번: 아기상어 2 / C++ (0) | 2021.09.17 |
[백준] 16953번: A -> B / C++ (0) | 2021.09.16 |
[백준] 2606번: 바이러스 / C++ (0) | 2021.09.16 |
댓글