POJ 3728 Catch That Cow (广搜)
来源:互联网 发布:二维码生成器源码 编辑:程序博客网 时间:2024/05/16 15:01
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 42564 Accepted: 13225
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
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.
#include<cstdio>#include<string.h>#include<iostream>#include<queue>#include<algorithm>using namespace std;int vis[100010];int n,k;struct stu{ int ans; int sum;};int bfs(){ queue <stu> q;// while(q.size())// q.pop(); memset(vis,0,sizeof(vis)); stu kaishi={n,0}; q.push(kaishi); vis[kaishi.ans]=1; while(q.size()) { stu xian=q.front(); stu xia; q.pop(); int i; for(i=0;i<3;i++) { if(i==0) xia.ans=xian.ans+1; if(i==1) xia.ans=xian.ans-1; if(i==2) xia.ans=xian.ans*2; xia.sum=xian.sum+1; if(xia.ans==k) return xia.sum; if(xia.ans>=0&&xia.ans<=100010&&!vis[xia.ans]) { vis[xia.ans]=1; q.push(xia); } } } return 0;}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { if(n>=k) { printf("%d\n",n-k); } else printf("%d\n",bfs()); } return 0;}
0 0
- POJ 3728 Catch That Cow (广搜)
- poj 3278 Catch That Cow 广搜
- -----广搜 POJ 3278-Catch That Cow
- 广搜:Catch That Cow
- poj 3278 Catch that cow(BFS 广搜)
- poj 3278 Catch That Cow(简单的广搜+剪枝)
- POJ-3278 Catch That Cow(广搜+剪枝)
- poj 3278 Catch That Cow(广搜)
- [搜索] POJ 3278 Catch that Cow 广搜的讨论
- poj 3287 -- Catch That Cow (bfs 广搜)
- poj 3278 Catch That Cow(bfs广搜)
- POJ-3278-Catch That Cow-广搜(BFS)
- POJ 3278 / hdu 2717 Catch That Cow (广搜)
- POJ 3278 Catch That Cow(广搜BFS)
- POJ-3278&&HDU-2717--Catch That Cow---BFS广搜
- PKU3278 Catch That Cow 基础广搜
- pku 3278 Catch That Cow(广搜)
- HLG 1012 Catch that cow (广搜)
- strcpy error
- 排序算法——归并排序
- Cannot configure two CRS instances on the same cluster
- Web:检查是否是合理的URL
- boost正则库匹配ASII编码的中文、全角字符示例
- POJ 3728 Catch That Cow (广搜)
- visio中如何添加外键约束
- Lua5.1编程一:Lua语言基础
- 输入一串以#为结束的字母,统计出现的次数,并用冒泡法排序?
- android SAX解析XML
- ZZ测试的三重境界
- 用vs.net2010做flex/flash/as3开发
- ZOJ 3706 Break Standard Weight(深搜处理)
- Eclipse快捷键 10个最有用的快捷键