#include <iostream>
#include <queue>
#define MAX 100001
using namespace std;
int N,K; //N=์๋น์ด๊ฐ ์๋ ์์น, K=๋์์ด ์๋ ์์น
int visited[MAX];
int ans=MAX;
void bfs(int start,int time){
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq; //{์๊ฐ,ํ์ฌ ๊ฑฐ๋ฆฌ}
pq.push({0,start});
visited[start]=1;
while(!pq.empty()){
int time=pq.top().first;
int start=pq.top().second;
pq.pop();
visited[start]=1;
if(start==K){
ans=time;
return;
}
else{
if(start>=0&&start-1<MAX&&!visited[start-1]) pq.push({time+1,start-1}); //๊ฑธ์๋ ์๊ฐ +1
if(start+1<MAX&&!visited[start+1]) pq.push({time+1,start+1}); //๊ฑธ์๋ ์๊ฐ +1
if(start*2<MAX&&!visited[start*2]) pq.push({time,start*2}); //์๊ฐ์ด๋ ํ ๋ ์๊ฐ +0
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>N>>K;
bfs(N,0);
cout<<ans;
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 11497๋ฒ - ํต๋๋ฌด ๊ฑด๋๋ฐ๊ธฐ (0) | 2021.11.05 |
---|---|
[๋ฐฑ์ค,c++] 1357๋ฒ - ๋ค์งํ ๋ง์ (0) | 2021.11.04 |
[๋ฐฑ์ค,c++] 1356๋ฒ - ์ ์ง ์ (0) | 2021.11.04 |
[๋ฐฑ์ค,c++] 13420๋ฒ - ์ฌ์น์ฐ์ฐ (0) | 2021.11.04 |
[๋ฐฑ์ค,c++] 13414๋ฒ - ์๊ฐ์ ์ฒญ (0) | 2021.11.04 |
[๋ฐฑ์ค,c++] 1330๋ฒ - ๋ ์ ๋น๊ตํ๊ธฐ (0) | 2021.11.04 |
๋๊ธ