每天3题,一题解题报告@yewleb

来源:互联网 发布:怎样添加网络连接 编辑:程序博客网 时间:2024/06/01 10:24
Problem Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?
 

Input
Line 1: Two space-separated integers: N and K
 

Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
 

Sample Input
5 17
 

Sample Output
4
#include<cstdio>#include<algorithm>#include<queue>#include<cstring>using namespace std;const int N = 100000;int n,k,dist[N+10];bool vis[N+10];int bfs(int x,int y){   queue<int>q;   q.push(x);   while(!q.empty())   {      int t=q.front();      q.pop();      if(t == y)      {          return dist[t];      }//wa在此,如果用我的代码无法通过相同数据的问题。敲打      int nt=t-1;      if(nt>0 && !vis[nt])      {        q.push(nt);        vis[nt] = 1;        dist[nt]=dist[t]+1;      }      nt=t+1;      if(nt<=N && !vis[nt])      {          q.push(nt);          vis[nt] = 1;          dist[nt]=dist[t]+1;      }       nt=t*2;      if(nt<=N && !vis[nt])      {          q.push(nt);          vis[nt] = 1;          dist[nt]=dist[t]+1;      }   }}int main(){    while(scanf("%d%d",&n,&k)!=EOF)    {        memset(dist, 0, sizeof dist );        memset(vis, 0, sizeof vis );        int ans=bfs(n,k);        printf("%d\n", ans);    }    return 0;}


0 0