1. 题目源地址:http://poj.org/problem?id=3278
2. 源代码:
//POJ--3278:Catch That Cow
#include<iostream>
#include<memory.h>
#include<queue>
#define MAX 100010
using namespace std;
int N,K;
int visited[MAX];
struct node
{
int x;
int step;
}start,current,next;
int BFS()
{
queue<node> Q;
int i;
start.x=N;
start.step=0;
visited[start.x]=1;
Q.push(start);
while(!Q.empty())
{
current=Q.front();//取队头元素
Q.pop();//队头元素出栈
for(i=0;i<3;i++)
{
if(i==0)
next.x=current.x-1;
else if(i==1)
next.x=current.x+1;
else if(i==2)
next.x=current.x*2;
next.step=current.step+1;
if(next.x==K) return next.step;
if(next.x>=0 && next.x<=MAX && !visited[next.x])
{
Q.push(next);
visited[next.x]=1;
}
}
}
return 0;
}
int main()
{
int ans;
cin>>N>>K;
memset(visited,0,sizeof(visited));
if(N>=K) ans=N-K;
else ans=BFS();
cout<<ans<<endl;
return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/163037.html