博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2717.Catch That Cow
阅读量:5288 次
发布时间:2019-06-14

本文共 2415 字,大约阅读时间需要 8 分钟。

Catch That Cow
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit

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.
由于移动方式只有 +1 -1 *2 因此如果 起点大于终点,直接相减即可 剩下就是标准的BFS模板
1 /* 2 By:OhYee 3 Github:OhYee 4 Email:oyohyee@oyohyee.com 5 Blog:http://www.cnblogs.com/ohyee/ 6  7 かしこいかわいい? 8 エリーチカ! 9 要写出来Хорошо的代码哦~10 */11 12 #include 
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 #include
20 #include
21 #include
22 using namespace std;23 24 //DEBUG MODE25 #define debug 026 27 //循环28 #define REP(n) for(int o=0;o
v)36 return s - v;37 38 queue
Q;39 bool visited[maxn];40 memset(visited,false,sizeof(visited));41 int dis[maxn];42 memset(dis,0,sizeof(dis));43 44 Q.push(s);45 visited[s] = true;46 while(!Q.empty()) {47 int th = Q.front();48 Q.pop();49 50 //达到终点51 if(th == v)52 break;53 54 //拓展节点55 56 #define push \57 if(next > maxn || next <= 0) \58 continue;\59 if(!visited[next]) {\60 Q.push(next);\61 visited[next] = true;\62 dis[next] = dis[th] + 1;\63 }\64 65 66 int next;67 next = th + 1;68 push;69 next = th - 1;70 push;71 next = th * 2;72 push;73 }74 75 if(dis[v])76 return dis[v];77 else78 return -1;79 }80 81 bool Do() {82 int s,v;83 if(scanf("%d%d",&s,&v)==EOF)84 return false;85 printf("%d\n",BFS(s,v));86 return true;87 }88 89 int main() {90 while(Do());91 return 0;92 }
 

 

 
 

转载于:https://www.cnblogs.com/ohyee/p/5389479.html

你可能感兴趣的文章
IClient for js开发之地图的加载
查看>>
用css画三角形(提示框三角形)
查看>>
Uber中国在地方城市的人员架构是怎样的?
查看>>
再来一篇装逼老文章:屏幕传输算法
查看>>
Delphi 7下最小化到系统托盘
查看>>
抖动代码
查看>>
lsblk请参阅块设备
查看>>
SVM-SVM概述
查看>>
STL algorithm算法lower_bound和upper_bound(31)
查看>>
linux系统下怎么安装.deb文件?
查看>>
javascript常见编程模式举例
查看>>
列出man手册所有函数的方法
查看>>
[从jQuery看JavaScript]-匿名函数与闭包(Anonymous Function and Closure)【转】
查看>>
VisualStudio 常用快捷键-整理
查看>>
netty研究【1】:编译源代码
查看>>
GTK接口定义和实现
查看>>
Hadoop生态系统介绍
查看>>
uva 11468 Substring
查看>>
SoapUI、Jmeter、Postman三种接口测试工具的比较分析
查看>>
Android平台实现与Apache Tomcat服务器数据交互(MySql数据库)
查看>>