[POJ_3278]Catch That Cow
来源:互联网 发布:中文翻译拼音拍摄软件 编辑:程序博客网 时间:2024/06/08 07:09
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 58049 Accepted: 18049
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.已知起点位置N和终点位置K
2.已知起点运动的规律以及终点固定
3.求从N出发到K的最小时间
思路:
题目为BFS(广度优先算法)的典型应用,使用BFS方法逐步计算各种可能性,当起点运动位置到达终点时,完成便利,返回需要时间
代码如下:
#include <stdio.h>#define Max 1000000int que[Max+1]; //代表位置int time[Max+1]; //代表经过时间int n, k;bool isPass(int pos){if(pos > 0 && pos < Max+1 && !time[pos]) return true;else return false;};int CatchTheCow(){int rear = 0, front = 0;int cur,next;if(n >= k)return (n-k);que[rear ++] = n;while(front <= rear){cur = que[front ++];if(cur == k) return (time[cur]);for(int i = 0; i < 3; i++){if(i == 0)next = cur - 1;else if(i == 1)next = cur + 1;else next = cur*2;if(isPass(next)){time[next] = time[cur] + 1;que[rear ++] = next;}}}}int main(void){while(scanf("%d%d",&n,&k) != EOF){for(int i=0; i <= Max+1; i++)time[i] = 0;printf("%d\n", CatchTheCow());}return 0;}
0 0
- [POJ_3278]Catch That Cow
- Catch that cow(POJ_3278)
- POJ_3278 Catch That Cow
- POJ_3278 Catch That Cow(BFS)
- Poj_3278 Catch That Cow(BFS)
- POJ_3278 Catch That Cow 解题报告
- poj_3278 Catch That Cow(bfs)
- POJ_3278 Catch That Cow (bfs)
- POJ3278 Catch That Cow
- Catch That Cow
- Catch That Cow
- poj3278 Catch That Cow
- 3278. Catch That Cow
- 【HDU2717】-Catch that cow
- Catch That Cow
- F - Catch That Cow
- poj3278 - Catch That Cow
- HDOJ Catch That Cow
- 斐波那契博弈(Fibonacci Nim)
- /etc/profile、~/.bash_profile等几个文件的执行过程
- SAT写作的常考话题总结与分析
- JVM解读(一):JVM体系结构
- Windows下Nginx的安装与配置
- [POJ_3278]Catch That Cow
- 网站用户密码加密
- iOS解决两个静态库的冲突 duplicate symbol
- WebSphere Application Server 动态高速缓存技术教程
- java类的加载以及初始化顺序
- 【小松教你手游开发】【unity实用技能】Unity NGui 利用UIToggle做个分页标签
- 动态规划
- 【iOS开发系列】微信支付
- Spring整合RMI