///题意:大牛怎样最快的抓到小牛;给你大牛和小牛的坐标,大牛怎样才能最快的抓到小牛
///说来大牛的走位也真是神奇(腿长一点走当前坐标的两倍,短一点就往后退一步或者前进一步) 但是那些走都算一步;
///思路:一维的bfs走一走
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=1e7+7;
struct node {int x,step;};///位置和大牛走的步数
int n,m;
bool vis[maxn];
void bfs(int n)
{
memset(vis,false,sizeof(vis));
node now,next;
now.x=n;
now.step=0;
vis[now.x]=true;
queue<node>Q;
Q.push(now);
while(!Q.empty())
{
now=Q.front(),Q.pop();
vis[now.x]=true;
if(now.x==m) {printf("%d\n",now.step);return ;}
///三种走法
if(now.x>=1&&!vis[now.x-1])
{
next.x=now.x-1;
next.step=now.step+1;
vis[next.x]=true;
Q.push(next);
}
if(now.x+1<=m&&!vis[now.x+1])
{
next.x=now.x+1;
next.step=now.step+1;
vis[next.x]=true;
Q.push(next);
}
///存在2倍的情况时候自只能是小牛在大牛的前面;
if(now.x*2<=2*m&&!vis[now.x*2])///最开始的写法是now.x*2<=m ,WA WA WA~~~,然后组了几个样例发现是可以越界的而且是2*m;
{
next.x=now.x*2;
next.step=now.step+1;
vis[next.x]=true;
Q.push(next);
}
}
}
int main ()
{
while(~scanf("%d %d",&n,&m))
{
if(n>=m) {printf("%d\n",n-m);continue;}///大牛腿短的时候,它只能往后退,可以节省一部分时间;
bfs(n);
}
return 0;
}
POJ - 3278抓牛牛
最新推荐文章于 2022-03-07 20:38:17 发布