poj3278 hdu2717 Catch That Cow 广度优先搜索
来源:互联网 发布:五黄煞算法口诀 编辑:程序博客网 时间:2024/05/22 06:56
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3336 Accepted Submission(s): 1106
Problem 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?
* 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
4HintThe 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 和k,用 2*n和 n+1以及n-1三种操作得到 k,并且 输出最小步数。这里有一个小 陷阱,而且很容易发现(因为给的事例数据都过不了),就是如果当前数小于k,也是可以做n-1这个操作的,不要认为不可以。比如10到18,如果把 n-1再乘以2,则只需要两步,如果直接乘以2,再减两次1是三步。这个代码最核心的一点就是要利用标记,来消除已经被访问数据的操作,所以定义visit数组,从而减少时间消耗。当然是用过程中要判断是否超过边界,不然会RE的。本来代码写得很简洁,把判断推出的条件放在了出队列的地方,后来觉得这样浪费时间,所以把判断退出放在了每步操作之后,虽然代码复杂了,但是时间确实减少了188-110=78MS。另外唠叨两句,就是同样的题目,时间限制都是2S,同样的代码,在POJ 测试 110MS ,在 HDU 15MS,突然觉得还是POJ测试数据强大。下面是代码。#include<iostream>#include<queue>using namespace std;const int maxn=100000;int n,k;struct tnode{ int num; int s;};int visit[maxn+1];int bfs(){ memset(visit,0,sizeof(visit)); queue<tnode> q; tnode p,t; p.num=n;p.s=0; q.push(p); while(!q.empty()) { p=q.front(); q.pop(); t.s=p.s+1; if (p.num<k) { t.num=p.num+1; if (!visit[t.num]) { if (t.num==k) return t.s; q.push(t); visit[t.num]=1; } t.num=p.num*2; if (t.num<=maxn&&!visit[t.num]) { if (t.num==k) return t.s; q.push(t); visit[t.num]=1; } t.num=p.num-1; if (t.num>=0&&!visit[t.num]) { if (t.num==k) return t.s; q.push(t); visit[t.num]=1; } } else { t.num=p.num-1; if (t.num>=0&&!visit[t.num]) { if (t.num==k) return t.s; q.push(t); visit[t.num]=1; } } } return 0;}int main(){ while(scanf("%d%d",&n,&k)!=EOF) { if (n==k) printf("0\n"); else { printf("%d\n",bfs()); } } return 0;}
- poj3278 hdu2717 Catch That Cow 广度优先搜索
- POJ3278 - Catch That Cow - 广度优先搜索
- Catch That Cow-广度优先搜索-POJ3278
- POJ3278 Catch That Cow 广度优先搜索版
- (广度优先搜索)算法题-poj3278 Catch That Cow
- POJ3278(BFS广度优先搜索)之Catch That Cow
- POJ3278 HDU2717 Catch That Cow
- HDU2717(POJ3278):Catch That Cow
- 模板题之广度搜索 HDU2717 Catch That Cow
- 广度优先搜索bfs与抓住那头奶牛(Catch that cow, poj3278)
- POJ3278 Catch That Cow题解 广度优先搜索+限定解空间
- hdu2717 Catch That Cow(搜索)
- poj3287 Catch That Cow(广度优先搜索)
- Catch That Cow-----广度优先搜索
- Catch That Cow(广度优先搜索_bfs)
- Catch That Cow广度优先搜索
- 【HDU2717】-Catch that cow
- HDU2717:Catch That Cow
- 流量控制--滑动窗口
- socket
- 游戏中的光影效果
- Tga图片格式分析以及程序实现
- android 优秀开源项目收集
- poj3278 hdu2717 Catch That Cow 广度优先搜索
- 延时光照
- 结构体
- oracle回闪操作
- 寒霜2引擎的光照系统确实不是盖的
- zoj 3203 Light Bulb(三分)
- 变量的分类
- hdu 4414 Finding crosses 简单搜索
- 游戏引擎与游戏美术的关系