openjudge 抓住那头牛

来源:互联网 发布:网络舆情的监测技术 编辑:程序博客网 时间:2024/04/29 09:50

2971:抓住那头牛

  • 查看
  • 提交
  • 统计
  • 提问
总时间限制: 
2000ms 
内存限制: 
65536kB
描述

农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:

1、从X移动到X-1或X+1,每次移动花费一分钟
2、从X移动到2*X,每次移动花费一分钟

假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?


输入
两个整数,N和K
输出
一个整数,农夫抓到牛所要花费的最小分钟数
样例输入
5 17
样例输出
4
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int n,m,i,j;int p[10000000],a[10000000],t1[10000000];int head,tail;int main(){scanf("%d%d",&n,&m);    head=0; tail=1; a[n]=1; p[tail]=n;    while (head<tail)     {      head++;      int x=p[head];      if (x==m)       {       cout<<t1[head]<<endl;       return 0;       }      if (a[x-1]==0&&x-1>=0)   {    tail++;  p[tail]=x-1; t1[tail]=t1[head]+1; a[x-1]=1;   }  if (a[x+1]==0&&x+1<=100000)   {    tail++;  p[tail]=x+1; t1[tail]=t1[head]+1; a[x+1]=1;   } if (a[2*x]==0&&2*x<=100000)   {    tail++; p[tail]=2*x; t1[tail]=t1[head]+1; a[2*x]=1;    }     }} 

0 0