pda_pro12   3년 전

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
using namespace std;

int i, j, N,M;

int next1, next2, next3; 

int visit[100001];

struct DATA {
int cnt; 
int x; 
};

int main()
{
queue<DATA> block; 
cin >> N >> M; 

DATA d; 

d.x = N; 
d.cnt = 0; 
block.push(d);

while (!block.empty()) {
int x1, cnt1; 

x1 = block.front().x; 
cnt1 = block.front().cnt;

if (x1 == M){
cout << block.front().cnt;
return 0;
}

next1 = x1 + 1; 
next2 = x1 - 1; 
next3 = x1 * 2;

visit[x1] = 1; 

block.pop(); 

if (!visit[next1]) {
visit[next1] = 1;
d.x = next1; 
d.cnt = cnt1 + 1;
block.push(d);

}
if (!visit[next2]) {
visit[next2] = 1; 
d.x = next2; 
d.cnt = cnt1 + 1;
block.push(d);
}
if (!visit[next3]) {
visit[next3] = 1; 
d.x = next3; 
d.cnt = cnt1 + 1;
block.push(d);
}

}
return 0; 
}

댓글을 작성하려면 로그인해야 합니다.