poj 3278 Catch That Cow

来源:互联网 发布:linux 启动oracle 编辑:程序博客网 时间:2024/04/30 08:54
/*第一道广搜。。。一维广搜算是最简单的广搜了吧*/#define LOCAL#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<cctype>#include<iomanip>#include<string>#include<algorithm>#include<ctime>#include<stack>#include<queue>#include<vector>#define N 100000using namespace std;int main(){#ifdef LOCAL       freopen("input.txt","r",stdin);       freopen("output.txt","w",stdout);#endif   int start,end,path[N+5];//path[]记录走过的路和走的步数   while(cin>>start>>end)   {    queue<int> q;memset(path,-1,sizeof(path));path[start]=0;q.push(start);while(!q.empty())//遍历每个结点{start=q.front();q.pop();if(start-1>=0&&path[start-1]==-1){q.push(start-1);path[start-1]=path[start]+1;}if(start+1<=N&&path[start+1]==-1){q.push(start+1);path[start+1]=path[start]+1;}                if(start*2<=N&&path[start*2]==-1){q.push(start*2);path[start*2]=path[start]+1;}}cout<<path[end]<<endl;   }return 0;}

原创粉丝点击