您的位置:首页 > 编程语言

poj3278 Catch that cow 代码比很多人精简。。。BFS

2013-05-27 17:08 381 查看
在一条线上找羊,农夫可以向前或向后一步,还可以步数乘二的跳跃。

#include<iostream>
#include<queue>
#include<memory.h>
#include<time.h>
using namespace std;
int Line[100001];
int N,K;
queue<int> Q;
int main()
{
clock_t start,end;

cin>>N>>K;
start=clock();
memset(Line,0, sizeof(Line));
Q.push(N);
int at;
while(!Q.empty())
{
at=Q.front();Q.pop();
if(at==K)
{
cout<<Line[at]<<endl;
break;
}
if((at-1)>=0&&Line[at-1]==0)
{
Q.push(at-1);
Line[at-1]=Line[at]+1;
}
if((at+1)<=100000&&Line[at+1]==0)
{
Q.push(at+1);
Line[at+1]=Line[at]+1;
}
if(2*at<=100000&&Line[at*2]==0)
{
Q.push(2*at);
Line[at*2]=Line[at]+1;
}
}
end=clock();
cout<<(end-start)<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: