【搜索入门专题1】hdu2717 H
来源:互联网 发布:什么是奥菲斯软件 编辑:程序博客网 时间:2024/05/20 07:18
Catch That Cow
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
4
题意:输入起始点和终止点,输出起始点到终止点的步数,从起始点到终止点有三种走法,1:自身步数+1;2:自身步数-1;3:自身步数*2。
思路:从起始点开始,搜索三种走法能到达的点,满足条件的点加入队列,直到找到终止点,结束函数,输出步数。
易错点:判断该点是否走过之前,首先该点必须满足在数组范围内,RE+MEL 11遍 最后还是师父给指点迷津
#include<stdio.h>#include<iostream>#include<string.h>#include<queue>using namespace std;#define N 1001000int book[N];int startx,endx;int sumtime;struct note{int x;int time;};void BFS(){note head,now_head,next1,next2,next3;queue<note>Q;head.time = 0;head.x = startx;book[startx] = 1;Q.push(head);while(!Q.empty()){now_head = Q.front() ;Q.pop() ;if(now_head.x == endx)//找到终止点,结束函数 {sumtime = now_head.time ;return;}next1.x = now_head.x + 1;//向前走一步 next2.x = now_head.x - 1;//往后退一步 next3.x = now_head.x + now_head.x;//自身两倍 //注意步数范围//注意先后顺序,首先满足在范围内,再判断是否走过 if(next1.x < N&&next1.x>=0&&!book[next1.x]){book[next1.x] = 1;next1.time = now_head.time + 1;Q.push(next1); }if(next2.x < N&&next2.x >= 0&&!book[next2.x]){book[next2.x] = 1;next2.time = now_head.time + 1;Q.push(next2); }if(next3.x < N&&next3.x >= 0&&!book[next3.x]){book[next3.x] = 1;next3.time = now_head.time + 1;Q.push(next3); }}return;}int main(){while(scanf("%d%d",&startx,&endx)!=EOF){sumtime = 0;memset(book,0,sizeof(book));BFS();printf("%d\n",sumtime);}return 0;}
阅读全文
0 0
- 【搜索入门专题1】hdu2717 H
- 【搜索入门专题1】I
- 【搜索入门专题1】E
- 搜索专题 H
- 【最小生成树入门专题1】H
- 【最短路入门专题1】H
- 【二分图匹配入门专题1】H
- hdu2717[BFS+启发式搜索]
- 【搜索入门专题1】hdu1548 G
- 【搜索入门专题1】hdu1253 【BFS】 F
- 【搜索入门专题1】 hdu1242 J
- [kuangbin带你飞]专题1 简单搜索 H
- hdu2717 Catch That Cow(搜索)
- Search Algorithm-搜索入门专题
- 【搜索入门专题练习1】hdu1241+hdu1312 C+D【DFS】
- hdu2717
- hdu2717
- hdu2717
- hpuoj 【1032】字母的个数【水题】
- 牛腩新闻发布系统-宏观把控篇
- CSS radio美化
- MySQL索引原理及慢查询优化
- 在centos6.5系统下配置LAMP详解教程
- 【搜索入门专题1】hdu2717 H
- 弹出层效果
- SCU 4439 Vertex Cover (dfs +剪枝 一般图的点覆盖)
- 字符串全排列
- C++Primer Plus(第六版) 第十二章 第一题
- Javascript自定义错误,继承Error
- javascript的一道this综合题目
- css兼容总结
- PERL能为我做什么(1)