题目链接:http://poj.org/problem?id=3278
分析:广搜,每次三种情况枚举一下,太水不多说了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 100010
using namespace std;
struct node
{
int x,step;
bool operator<(const node a)const
{
return step>a.step;
}
};
priority_queue<node>que;
int vis[N];
int judge(int x)
{
return x>=&&x<=;
}
node make_node(int a,int b)
{
node temp;
temp.x=a;temp.step=b;
return temp;
}
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)>)
{
memset(vis,,sizeof(vis));
while(!que.empty())que.pop();
que.push(make_node(n,));
vis[n]=;
int ans=;
while(!que.empty())
{
node now=que.top();
que.pop();
int x=now.x;//printf("%d %d\n",now.x,now.step);
if(x==k)
{
ans=now.step;
break;
}
if(judge(x+)&&!vis[x+])
{
vis[x+]=;que.push(make_node(x+,now.step+));
}
if(judge(x-)&&!vis[x-])
{
vis[x-]=;que.push(make_node(x-,now.step+));
}
if(judge(*x)&&!vis[x*])
{
vis[x*]=;que.push(make_node(*x,now.step+));
}
}
printf("%d\n",ans);
}
}