#include <cstdio> #include <queue> | |
| #include <utility> | |
| using namespace std; | |
| int N, K; | |
| int bfs(void) | |
| { | |
| queue< pair<int, int> > q; | |
bool visit[100001]={0}; // 범위 주의 | |
| q.push({N,0}); | |
| while(!q.empty()) | |
| { | |
| int x = q.front().first; | |
| int t = q.front().second; | |
| q.pop(); | |
| if(x<0 || x>100000) continue; | |
| if(visit[x]) continue; | |
| visit[x]=1; | |
| if(x==K) return t; | |
| q.push({x*2,t+1}); | |
| q.push({x+1,t+1}); | |
| q.push({x-1,t+1}); | |
| } | |
| } | |
| int main() | |
| { | |
| scanf("%d %d", &N, &K); | |
| printf("%d", bfs()); | |
| return 0; | |
| } |
'알고리즘 문제 풀이 > 1DP_과제(~180615)' 카테고리의 다른 글
| 171115_2667_단지번호붙이기 (0) | 2017.11.15 |
|---|---|
| 171114_1012_유기농 배추 (0) | 2017.11.14 |
| 171113_1260_dfs와 bfs (0) | 2017.11.13 |
| 171112_2814_최장경로 (0) | 2017.11.12 |
| 171111_7576_토마토 (0) | 2017.11.11 |