POJ 3278 Catch That Cow
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 KOutput
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.Sample Input
5 17Sample Output
4Hint
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.
#include<string.h> #include<stdio.h> #include<queue> #include<stack> using namespace std;int road[200000];int main() {queue<int>q, step;int n,k,x0,st0;while (scanf("%d%d", &n, &k) != EOF){memset(road, -1, sizeof(road));while (q.size()) { q.pop(); step.pop(); }q.push(n);step.push(0);//road[n] = 0;while (q.size()){x0 = q.front();q.pop();st0 = step.front();step.pop();road[x0] = st0;if (x0 == k) { printf("%d\n", road[k]);break; }if (x0 - 1 >= 0 && road[x0 - 1] < 0) { q.push(x0 - 1);step.push(st0 + 1); }if (x0 +1 <= 100000 && road[x0 + 1] < 0) { q.push(x0 + 1);step.push(st0 + 1); }if (x0*2<=100000&& road[x0 *2] < 0) { q.push(x0 *2);step.push(st0 + 1); }}} }
转载于:https://www.cnblogs.com/914295860-jry/p/5822511.html
POJ 3278 Catch That Cow相关推荐
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30924 Accepted: 9536 D ...
- BFS POJ 3278 Catch That Cow
题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...
- 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 ...
- POJ 3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35043 Accepted: 10800 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- bfs+dfs分析----poj 3278 Catch That Cow
题目详情 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 115430 Accepted: ...
- poj 3278 Catch That Cow 广搜
hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...
- poj 3278 Catch That Cow(广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45087 Accepted: 14116 ...
- POJ - 3278 Catch That Cow 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
最新文章
- [原创]SQL 表值函数:获取从今天计算起往前自定义天数
- skb_shinfo(skb)-dataref 含义
- Java 8大原子操作
- action 带参数跳转
- SpringBoot学习之路:06.Spring Boot替换默认的Jackson
- [Codeforces Round #165 (Div. 2)]D. Greenhouse Effect
- 防止在多模块Maven中找到“未找到插件”
- 字符串中全角半角之间的转换
- 个人房贷为啥又贵又难贷 一个房贷银行有3套逻辑
- SVN遇到Can't convert string from 'UTF-8' to native encoding(转)
- python找不到文件中文文件名_找不到的方法虽然存在于同一个py文件中 - python
- vs资源视图加载失败
- OpenCV 访问Mat 像素
- 学python买什么书-Python爬虫入门看什么书好 小编教你学Python
- Ubuntu1404+Django1.9+Apache2.4部署配置1安装
- ignite mysql 持久化_2 ignite关键特性
- python实现isprime函数_Python实现isPrime函数
- 【JZOJ5336】【NOIP2017提高A组模拟8.24】提米树
- 声谱图(spectrogram)、FBank(Mel_spectrogram)和 MFCC(Mel倒谱)到底用哪个作为NN输入?
- diffuse、specular贴图的光照
热门文章
- 国内现在web前端高手薪资都拿多少?
- vue.js是什么框架?有什么优势?
- android在xml中加载大图,android – 如何在布局xml中添加循环视图
- props传递对象_vue-父组件传值props(对象)给子组件
- python实时连接oracle_python连接oracle数据库
- mysql跳板机_python实现ssh通过跳板机连接mysql
- jpa onetoone_java – JPA,Hibernate:仅使用外键的OneToOne映...
- PyTorch: torch.optim 的6种优化器及优化算法介绍
- TensorFlow神经网络(七)卷积神经网络基础
- scrapy 去重策略修改