您的位置:首页 > 其它

HDU-2717-Catch That Cow

2013-08-12 15:54 309 查看
//只有三种搜素情况
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int map[100010];
int k,n;
struct node
{
int x;
int time;
};
int check(int x)
{
if(x<0||x>100000||map[x])
return 0;
return 1;
}
int bfs(int x)
{
queue<node>q;
node a,b;
a.x=x;
a.time=0;
map[x]=1;
q.push(a);
while(!q.empty())
{
a=q.front();
q.pop();
if(a.x==k)
return a.time;
b=a;
b.x=a.x+1;
if(check(b.x))
{
b.time=a.time+1;
map[b.x]=1;
q.push(b);
}
b.x=a.x-1;
if(check(b.x))
{
b.time=a.time+1;
map[b.x]=1;
q.push(b);
}
b.x=2*a.x;
if(check(b.x))
{
b.time=a.time+1;
map[b.x]=1;
q.push(b);
}
}
return -1;
}
int main()
{
int ans;
while(cin>>n>>k)
{
memset(map,0,sizeof(map));
ans=bfs(n);
cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: