bfs—Catch That Cow—poj3278
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 87152 | Accepted: 27344 |
http://poj.org/problem?id=3278
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
Output
Sample Input
5 17
Sample Output
4
Hint
#include<iostream>
#include<queue>
#include<cstring>
using namespace std; const int MAX=;
int pace[MAX+];
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(pace,,sizeof(pace));
int t;
queue<int> qu;
qu.push(n);
while(!qu.empty())
{
t=qu.front();
if(t==k)
{
cout<<pace[t]<<endl;
return ;
}
qu.pop();
if(t<MAX&&!pace[t+])
{
qu.push(t+);
pace[t+]=pace[t]+;
}
if(t>&&!pace[t-])
{
qu.push(t-);
pace[t-]=pace[t]+;
}
if(t*<=MAX&&!pace[t*])
{
qu.push(t*);
pace[t*]=pace[t]+;
}
}
}
return ;
}
bfs—Catch That Cow—poj3278的更多相关文章
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- POJ 3287 (基础BFS) Catch That Cow
这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
随机推荐
- Java 判断日期的方法
//str:传入的日期 eg:"2018-07-23" function IsDate(str) { arr = str.split("-"); if(arr. ...
- Linux基础;Day07
dns服务 dns的作用:地址解析 IP -> 域名(反向) 域名 -> IP(正向) 类型 主域名服务器 负责维护一个区域的所有域名信息,是特定的所有信息的权威信息源,数据可以修改. ...
- SSM 三大框架系列:Spring 5 + Spring MVC 5 + MyBatis 3.5 整合(附源码)
之前整理了一下新版本的 SSM 三大框架,这篇文章是关于它的整合过程和项目源码,版本号分别为:Spring 5.2.2.RELEASE.SpringMVC 5.2.2.RELEASE.MyBatis ...
- win10无法使用VMwareWorkstation的解决办法
最近(2019-10)国庆期间,微软更新了一次win10. 此次更新导致很多同学的Workstation pro不能用了. 主要的解决办法有一下几种: 卸载最新的系统更新包 在控制面板>> ...
- Android Visibility控件显示和隐藏
Android控件显示和隐藏 visibility 可见(visible) XML文件:android:visibility="visible" Java代码:view.setVi ...
- 012-C语言小游戏之推箱子
012-C语言小游戏之推箱子 一.创建游戏地图 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #define ROWS 11 #define COLS 12 char ...
- stand up meeting 11/24/2015
part 组员 今日工作 工作耗时/h 明日计划 计划耗时/h 词典接口及数据转换 冯晓云 规范在线查词的各项请求,将返回结果解析成树状,并定义完成各种操作以方便其他部分完成调用,排序,增删等操作 3 ...
- Python - 调用接口合并文件夹下多个Excel表
在工作中经常遇到需要打开许多个excel表格,然后合并的需求,合并的同时要求格式必须原汁原味的保留.利用VBA代码可以比较轻松的解决,现在我们来看Python中如何实现. 上代码: from open ...
- Linux学习笔记(七)关机、重启及常用的网络命令
关机.重启命令 sync shutdown reboot init sync 英文原意:flush file system buffers 功能:刷新文件系统缓冲区,将内存中的数据保存到硬盘中 语法: ...
- 神奇的Kivy,让Python快速开发移动app
随着移动互联网的不断发展,手机.Pad等移动终端已经被普遍使用,充斥在人们的工作.学习和生活中,越来越多的程序都转向移动终端,各类app应用相拥而至. Kivy作为Python的Android和IOS ...