【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow
Descriptions:
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
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
农夫约翰获知了一头逃走的牛的位置,想要将它立即抓住。他从数轴上的一个点 N (0 ≤ N ≤ 100,000) 出发,牛位于相同数轴上的点 K (0 ≤ K ≤ 100,000)。农夫约翰有两种前进方式:行走和神行。
神行:农夫约翰可以从任何点 X 移动到点 2 × X,也只需要一分钟。
如果那头逃走的牛并不知道对它实施的抓捕行动,因此完全不移动,那么农夫约翰花多少时间可以抓住这头牛?
Input
第一行包含两个以空格分隔的整数: N 和 K
Output
第一行输出农夫约翰抓住逃走的牛,所花费的最短时间 (分钟)。
Hint
农夫约翰抓住逃走的牛的最快方式,是按如下路径移动:5-10-9-18-17,这花费了 4 分钟。
1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #include <string> 10 #include <cstring> 11 #include <map> 12 #include <stack> 13 #include <set> 14 using namespace std; 15 int N,K; 16 int vis[100010];//路径判断是否走过 17 struct node 18 { 19 int n,k;//n为现在的地方,k为牛的地方 20 int step;//步数即时间 21 friend bool operator<(node a,node b)//步数小的现出来,即时间少 22 { 23 return a.step>b.step; 24 } 25 }; 26 priority_queue<node>q; 27 void bfs() 28 { 29 node now;//初始化now 30 now.n=N; 31 now.k=K; 32 now.step=0; 33 q.push(now); 34 while(!q.empty()) 35 { 36 node mid=q.top(); 37 q.pop(); 38 if(mid.n==mid.k)//农夫现在的地方等于牛的地方,即找到牛了 39 { 40 cout << mid.step <<endl; 41 return; 42 } 43 //农夫的三种走法判断是否符合题意 44 if(mid.n+1>=0&&mid.n+1<=100000&&!vis[mid.n+1]) 45 { 46 node last;//更新队列 47 last.k=K; 48 last.n=mid.n+1; 49 last.step=mid.step+1;//步数+1 50 vis[mid.n+1]=1;//标记路径 51 q.push(last);//入队 52 } 53 if(mid.n-1>=0&&mid.n-1<=100000&&!vis[mid.n-1]) 54 { 55 node last; 56 last.k=K; 57 last.n=mid.n-1; 58 last.step=mid.step+1; 59 vis[mid.n-1]=1; 60 q.push(last); 61 } 62 if(mid.n*2>=0&&mid.n*2<=100000&&!vis[mid.n*2]) 63 { 64 node last; 65 last.k=K; 66 last.n=mid.n*2; 67 last.step=mid.step+1; 68 vis[mid.n*2]=1; 69 q.push(last); 70 } 71 } 72 } 73 int main() 74 { 75 memset(vis,0,sizeof(vis));//初始化路径都为0,即没有走过 76 cin >> N >> K; 77 vis[N]=1; 78 bfs(); 79 return 0; 80 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话