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

在一条线上找羊,农夫可以向前或向后一步,还可以步数乘二的跳跃。

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值