hdu 2717 (一维广搜)
来源:互联网 发布:mac国家缩写 编辑:程序博客网 时间:2024/06/16 12:17
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14027 Accepted Submission(s): 4293
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.
#include<iostream>#include<queue>#include<memory.h>using namespace std;int n,m;int vis[100005]={0};int dir[2]={-1,1};struct node{int x;int step;}s,e;void bfs(){queue<node>q;node x1,t;s.step=0;vis[s.x]=1;q.push(s);while(!q.empty()){t=q.front();//cout<<t.x<<endl;q.pop();if(t.x==e.x){cout<<t.step<<endl;return;}else{for(int i=0;i<2;i++){x1.x=t.x+dir[i];if(x1.x>=0&&x1.x<=100000&&vis[x1.x]==0){x1.step=t.step+1;vis[x1.x]=1;q.push(x1);}}x1.x=t.x*2;if(x1.x>=0&&x1.x<=100000){x1.step=t.step+1;vis[x1.x]=1;q.push(x1);}}}cout<<-1<<endl;}int main(){ while(cin>>n>>m) { memset(vis,0,sizeof(vis)); s.x=n; e.x=m; bfs();}}
0 0
- hdu 2717 (一维广搜)
- HDU 2717
- hdu-2717
- HDU 2717
- HDU-2717
- Hdu 2717
- HDU 2717
- HDU-2717
- hdu 2111(Saving HDU)
- hdu 2109(fighting hdu)
- HDU Saving HDU (贪心)
- hdu 2717 Catch That Cow(搜索)
- hdu(2717)Catch That Cow
- hdu 2717 Catch That Cow(bfs)
- hdu 2717 Catch That Cow(搜索)
- hdu 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow(BFS)
- HDU 2717 Catch That Cow (BFS)
- C++之引用
- 来说说深圳搬家公司网站的优化策略
- 谱聚类(spectral clustering) python可视化实现
- hadoop基础框架介绍
- 子沐教你Scrapy——浅谈Python爬虫
- hdu 2717 (一维广搜)
- 计算机图形学03一一OpenGL 线的形成解析 和 DDA算法
- java 写文件的三种方法比较
- 严重: IOException while loading persisted sessions: java.io.EOFException解决方案
- C++ boost 实例学习
- 算法笔记_070-BellmanFord算法简单介绍(Java)
- spring 注解 事务和声明式事务共存的情况下如何决定先后顺序
- ceil()向上取整与round()逢五进一
- 三生三世之 数据挖掘