catch the cow (P3278)
来源:互联网 发布:网络直播消费心理分析 编辑:程序博客网 时间:2024/05/02 20:13
一开始就用我习惯的queue的方法进行广搜,结果就后面重复的枝条太多了。
后来用状态记录就OK
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<sstream>#include<algorithm>#include<queue>using namespace std;struct my{int num,t;};int ans;int s,e;my go[300000];bool vist[300000];void bfs(){memset(vist,false,sizeof(vist));int head,tail;head=tail=0;vist[s]=true;go[head].num=s;go[head].t=0;my cur;while (head<=tail){my d=go[head++];if (d.num==e){ans=d.t;return;}if (d.num-1>=0 && !vist[d.num-1]){tail++;go[tail].num=d.num-1;go[tail].t=d.t+1;vist[d.num-1]=true;}if (d.num<=e && !vist[d.num+1]){tail++;go[tail].num=d.num+1;go[tail].t=d.t+1;vist[d.num+1]=true;}if (d.num<=e && !vist[d.num*2] && d.num){tail++;go[tail].num=d.num*2;go[tail].t=d.t+1;vist[d.num*2]=true;}}}int main(){int i,j,k;while (cin>>s>>e){bfs();cout<<ans<<endl;}return 0;}
Catch That Cow
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 34076 Accepted: 10496
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.
Source
- catch the cow (P3278)
- 重做catch the cow
- Catch The Cow
- HLG Catch The Cow
- openjudge2971 catch the cow
- POJ 3278 Catch The Cow
- poj 3278 catch the cow
- catch the cow(简单题)
- toj2825 catch the cow BFS典型题
- catch the cow 抓住小黄牛
- POJ-3278 Catch the cow(BFS)
- catch the cow(poj-3728)
- poj 3728 catch the cow (bfs)广搜
- POJ3278 Catch That Cow
- Catch That Cow
- Catch That Cow
- poj3278 Catch That Cow
- 3278. Catch That Cow
- DB2 9 认证:认证须知
- 20130221昨天是新历生日
- php学习小记(一)
- c++模板
- ubuntu linux 安装jdk
- catch the cow (P3278)
- C++编程命名规范
- Java网络编程从入门到精通(13):使用Socket类接收和发送数据
- css使用细节&浏览器兼容小记
- 把数据库文件写入csv文件和从csv文件中导入数据到数据库中
- float属性
- Android弹出拨号界面和拨打电话实现
- 看英文PDF利器 智器+ColorDict 辞典
- uCOS学习笔记(四)——事件标志组