Catch That Cow
Catch That Cow
Time Limit:2000MS Memory Limit:65536K
Total Submit:118 Accepted:30
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?
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
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
#include<iostream> #include<queue> using namespace std; #define M 100001 #define INF 10000000 struct POINT { int pos; int step; }now,next; queue<POINT>Q; bool visited[M]; int n,k; int bfs() { while(!Q.empty()) Q.pop(); now.pos=n; now.step=0; visited[now.pos]=true; Q.push(now); while(!Q.empty()) { now=Q.front(); Q.pop(); next=now; for(int i=0;i<3;i++) { if(i==0) next.pos=now.pos+1; if(i==1) next.pos=now.pos-1; if(i==2) next.pos=now.pos*2; next.step=now.step+1; if(next.pos==k) return next.step; if(next.pos<0||next.pos>M) continue; if(!visited[next.pos]) { visited[next.pos]=true; Q.push(next); } } } return INF; } int main() { while(cin>>n>>k) { memset(visited,false,sizeof(visited)); if(n<k) cout<<bfs()<<endl; if(n==k) cout<<0<<endl; if(n>k) cout<<n-k<<endl; } }
Catch That Cow相关推荐
- C - Catch That Cow POJ - 3278
C - Catch That Cow POJ - 3278 首先是大暴搜+最优化剪枝(当当前搜索情况不如已有答案优时,返回) #include<cstdio> #include<al ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35043 Accepted: 10800 ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30924 Accepted: 9536 D ...
- POJ 3278 Catch That Cow BFS
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 32071 Accepted: 9866 D ...
- POJ 3278 Catch That Cow(BFS)
题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Tot ...
- 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...
- POJ3278——Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 114140 Accepted: 35715 ...
- 动态规划训练13 [Catch That Cow poj3278]
Catch That Cow POJ - 3278 这道题我看大家用的方法都是bfs搜索,为什么在我看来这就是一个动态规划的题目啊啊啊啊啊啊啊 dp[x]表示从N出发到x所需要的最小时间 那么得到如下 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- Catch That Cow【广搜】
Catch That Cow POJ - 3278 Farmer John has been informed of the location of a fugitive cow and wants ...
最新文章
- PHP自动加载类—__autoload()和spl_autoload_register()
- 深度学习笔记 第四门课 卷积神经网络 第二周 深度卷积网络:实例探究
- android 串口一直打开_实例 | 使用Python串口实时显示数据并绘图
- 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)
- hdu4064 三进制状态压缩 好题!
- python3安装步骤-Mac安装python3的方法步骤
- Scrapy爬取小说简单逻辑
- uva 11234 Expressions
- spring中使用quartz(注解版)
- 一位原码的乘法规则_原码一位乘法的实现算法
- 图的深度优先遍历算法
- html5网页制作心得体会,学习网页制作心得体会范文3篇
- ACwing166数独与183靶形数独
- Python人脸识别黑科技(二):教你使用python+Opencv完成人脸解锁
- 小学计算机网络信息安全教案,黑教版信息技术五年级上册第十五课《网络信息安全》教案.doc...
- c++笔记③1109
- 3.2 从关系型数据库中查询数据
- [墨者学院] 日志文件分析溯源(脱裤的IP地址)
- TOP Network 2019年度回顾:积蓄力量,再创辉煌
- C语言项目二:电阻并联