博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3278 <Catch That Cow>
阅读量:5156 次
发布时间:2019-06-13

本文共 1991 字,大约阅读时间需要 6 分钟。

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.
 
 
 
 
 
题意:a走到b,3种移动方式。
题解:搜索。
AC代码:
#include
#include
#include
#include
using namespace std;int road[200000];int main(){ queue
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

你可能感兴趣的文章
mysql安装 demo [linux centos7] [5.7.26]
查看>>
ubuntu 16.04 搭建无线共享热点(PC 无线直连Android移动终端 调试,监控屏幕)
查看>>
Java EE 架构设计——基于okhttp3 的网络框架设计
查看>>
东软软件动态生成对数据表更新操作的方法
查看>>
Add Digits
查看>>
第二章 Mablab语言基础
查看>>
【Luogu】P1607庙会班车Fair Shuttle(线段树+贪心)
查看>>
crash reporting system for Windows applications
查看>>
系统启动正常,进入桌面时黑屏,可以看到鼠标
查看>>
随意谈谈tcp
查看>>
进制转换
查看>>
Linux 下五个顶级的开源命令行 Shell
查看>>
Linux平台中使用PHP让word转pdf
查看>>
03循环结构
查看>>
docker数据卷的使用 -v --volumes--from
查看>>
电磁兼容培训文稿
查看>>
初始化 静态代码块1
查看>>
shell 实现txt转换成html
查看>>
sqlserver 2008修改数据库表的时候错误提示“阻止保存要求重新创建表的更改”...
查看>>
53. (待补) (使用单链表)实现简单的管理系统 MVC 将链表作为内存数据模型,将文件作为数据库,将终端作为交互界面。读文件生成 链表,修改链表写入文件。...
查看>>