概述
Catch That Cow
HDU - 2717
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.
* 行走:FJ 可以在一分钟内从 X 点移动到 X - 1 或 X + 1 点
* 传送:FJ 可以在一分钟内 从 X 点移动到 2 × X 点。
如果母牛不知道它的追逐,根本不动,农夫约翰需要多长时间才能取回它?
输入
第 1 行:两个空格分隔的整数:N 和 K
输出
第 1 行:Farmer John 抓住逃亡母牛所需的最少时间(以分钟为单位)。
Sample Input
5 17
Sample Output
4
这道题 和A strange lift(奇怪的电梯) HDU - 1548 差不多,道理都一样,都是利用队列和bfs ????
有兴趣的话可以做一下⬆题,下面是????题的我的博客
https://blog.csdn.net/m0_58245389/article/details/119240688
本题AC
#include<stdio.h> #include<queue> #include<math.h> #include<string.h> #include<iostream> int v[100010]; using namespace std; struct Node { int space,step; }; int main(void) { int n,m,b; Node now,next; queue<Node>Q; while(cin>>n>>m) { memset(v,0,sizeof(v)); while(!Q.empty()) { Q.pop(); } now.space=n; now.step=0; Q.push(now); v[now.space]=1; while(!Q.empty())//分三路走 { now=Q.front(); Q.pop(); if(now.space==m) { printf("%dn",now.step); break; } b=now.space*2; if(b<=100000&&!v[b]) { v[b]=1; next.space=b; next.step=now.step+1; Q.push(next); } b=now.space+1; if(b<=100000&&!v[b]) { v[b]=1; next.space=b; next.step=now.step+1; Q.push(next); } b=now.space-1; if(b>=0&&!v[b]) { v[b]=1; next.space=b; next.step=now.step+1; Q.push(next); } } //printf("%dn",); } return 0; }
最后
以上就是聪明野狼为你收集整理的Catch That Cow-抓住那头牛(BFS+队列) Catch That Cow HDU - 2717 的全部内容,希望文章能够帮你解决Catch That Cow-抓住那头牛(BFS+队列) Catch That Cow HDU - 2717 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复