原题是The 35th ACM-ICPC Asia Regional Contest (Hangzhou) 杭州区域赛的题,杭电OJ上题目中数据应该为10的500次方,但显示的是10500所以这题很坑爹,相比POJ的这道题完全把数据降低了,暴力BFS完全没问题!
#include<cstdio>
#include<cstring>#include<cmath>
#include<cctype>
#include<cstdlib>
#include<algorithm>
#include<queue>
#define MAXN 100010
struct Node{
int value;
int depth;
};
using namespace std;
queue <Node> q;
bool vis[MAXN];
int n,k;
inline bool Judge(int x)
{
if(x < 0 || x > MAXN || vis[x]) return false;
return vis[x] = true;
}
int BFS()
{
Node a;
a.depth = 0,a.value = n;
q.push(a);
while(true)
{
a = q.front();
q.pop();
if(a.value==k) return a.depth;
else
{
Node b;
b.value = a.value + 1;
b.depth = a.depth + 1;
if(Judge(b.value)) q.push(b);
b.value = a.value - 1;
if(Judge(b.value)) q.push(b);
b.value = a.value * 2;
if(Judge(b.value)) q.push(b);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(scanf("%d %d",&n,&k)==2)
{
memset(vis,false,sizeof(vis));
printf("%d\n",BFS());
}
return 0;
}