现在的位置: 首页 > 综合 > 正文

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

2013年05月04日 ⁄ 综合 ⁄ 共 572字 ⁄ 字号 评论关闭
在一条线上找羊,农夫可以向前或向后一步,还可以步数乘二的跳跃。

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

抱歉!评论已关闭.