POJ_3278_ Catch That Cow _广搜
2016-03-05 11:57
330 查看
/*
B - Catch That Cow
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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
*/
B - Catch That Cow
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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 <stdio.h> #include <string.h> #include <queue> #define Max 200005 int used[Max]; using namespace std; struct Node { int n; int num; }; int main() { int N,K; while(scanf("%d%d",&N,&K)!=EOF) { memset(used,0,sizeof(used)); queue<Node>Q; Node start; start.n = N; start.num = 0; Q.push(start); while(!Q.empty()) { start = Q.front(); Q.pop(); int a,b; a = start.n; b = start.num; if(a==K) { break; } if(a<K && !used[a*2]) { used[a*2] = 1; start.n = a * 2; start.num = b + 1 ; Q.push(start); } if(a>0 &&!used[a-1]) { used[a-1] = 1; start.n = a - 1; start.num = b + 1 ; Q.push(start); } if(a<K && !used[a+1]) { used[a+1] = 1; start.n = a + 1; start.num = b + 1 ; Q.push(start); } } printf("%d\n",start.num); } return 0; }
相关文章推荐
- UVA11136Hoax or what( multiset的应用)
- 7-1-图、表的数组(邻接矩阵)表示法-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版
- 微信企业号平台开发之获取菜单,创建菜单和删除菜单
- TRIM:提升磁盘性能,缓解Android卡顿
- 设计模式---单例模式
- 欢迎使用CSDN-markdown编辑器
- 取模运算和求余运算的区别
- js 对象与函数的区别
- JavaScript 之理解closure(闭包)
- ExecutorService
- ListViewDemo
- hdu1027
- java小程序
- 如何查看 apahce 已经加在的模块
- iOS:App Extension Keyboard自定义高度
- IT十八掌作业_java基础第二天_进制转换原理和补码存储方式作业
- Git详解之二 Git基础
- Lua_第 14 章 Packages
- 使用基于ThinkPHP3.2.3的ThinkAdmin创建手机电脑通用的表白墙(二)自动判断设备以显示不同界面
- nyoj 合并游戏(状态压缩dp)