POJ 3278 抓牛简单广搜
来源:互联网 发布:产品标贴制作软件 编辑:程序博客网 时间:2024/04/30 14:47
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 75331 Accepted: 23781
Description
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX+ 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 andK
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.
如Hint中所说,在一条线上抓,输入的N是人的起始位置,K是牛的人要到达的位置
如Hint中所说,在一条线上抓,输入的N是人的起始位置,K是牛的人要到达的位置
#include <iostream>#include <queue>#include <cstdio>#include <cstring>using namespace std;const int maxn = 100050;int main(){ queue<int> a; int vis[maxn]; int n,k; cin>>n>>k; memset(vis,0,sizeof(vis)); int flag = 0; vis[n]=1; int step = 0; a.push(n);if(n!=k){ //要考虑这种情况,不然0步输出的是1步while(!a.empty()&&!flag){ int t = a.size();//队列中元素的个数 step++; while(t--){ //参考比较模板的while(head<tail),这里直接用的STL,所以形式变了 int next,nnext[3]; next = a.front(); nnext[0]=next+1; nnext[1]=next-1; nnext[2]=next*2; a.pop(); for(int i = 0;i<3;i++){ if(nnext[i]==k){ flag=1; break;} if(nnext[i]>=1&&nnext[i]<=100000&&!vis[nnext[i]]){ vis[nnext[i]]=1; a.push(nnext[i]); } } if(flag) break;//我觉得这一步能省时间的,可是去掉不去掉提交的时间都一样,可能数据比较小吧 //当n=1,k=2时,在step=1时,进栈的顺序是2,0,0,找到第一个2直接退出,因为此时step都是1 } }}cout<<step<<endl;return 0;}
0 0
- POJ 3278 抓牛简单广搜
- POJ 3278 简单广搜
- POJ 3278(简单广搜)
- poj 3278 简单的广搜入门
- poj 3278 广搜
- poj 3278 Catch That Cow(简单的广搜+剪枝)
- poj 1164 简单地广搜
- poj 3087 Shuffle'm Up(简单的广搜)
- POJ 2251 Dungeon Master(三维简单广搜)
- 7.12 POJ 3278 A - 广搜 基础
- poj 3278 Catch That Cow 广搜
- -----广搜 POJ 3278-Catch That Cow
- 广搜 poj 3126
- POJ 3126 广搜
- POJ 2049 广搜
- 广搜poj.3009
- POJ 2243 广搜
- poj-2251 广搜
- android 6.0的权限问题
- Codeforces #280 Div.2 E.Vanya and Field 数学,数论
- Android扫描枪(PDA)开发
- 心血来潮,小试c++11
- rf中对于有滚动条的页面中元素的处理
- POJ 3278 抓牛简单广搜
- 个人总结——studio的常用插件的使用(工作实践)
- 基于QT实现对网络摄像头数据的显示
- 深入理解HashMap(及hash函数的真正巧妙之处)
- Cloudera Manager 5安装
- 配置自定义标签可以有三种方式
- POJ 1611 The Suspects (并查集)
- 关于android自定义控件Topbar的学习
- 【Android基础知识】Activity的四种启动模式