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 ...
随机推荐
- 怎样用scratch2.0谱写音乐
打开scratch2.0将语言切换为简体中文: 如果需要播放特殊的声音,可以用播放声音,找到一些特有的音乐,或者通过录制,将自己的配音或者唱歌录制下来: 可以用弹奏鼓声命令弹奏各种击鼓音乐: 通过控制 ...
- 让 .NET 轻松构建中间件模式代码
让 .NET 轻松构建中间件模式代码 Intro 在 asp.net core 中中间件的设计令人叹为观止,如此高大上的设计何不集成到自己的代码里呢. 于是就有了封装了一个简单通用的中间件模板的想法, ...
- Flask 入门(五)
jinjia2模板传参 在html中调用python代码中传入的参数规则己经在上文中说明白了,下面,我们来实用一下: 1.编辑index.py中的代码如下: from flask import Fla ...
- MAC中PHP7.3安装mysql扩展
1.下载mysql扩展http://git.php.net/?p=pecl/database/mysql.git;a=summary 2.解压tar xzvf mysql-d7643af.tar.gz ...
- 35 编码 ASCII Unicode UTF-8 ,字符串的编码、io流的编码
* 编码表: * 信息在计算机上是用二进制表示的,这种表示法让人理解就很困难.为保证人类和设备,设备和计算机之间能进行正确的信息交换,人们编制的统一的信息交换代码,这就是ASCII码表 *ASCII ...
- 8.2 常见api:String类的使用
/* * String:字符串类 * 由多个字符组成的一串数据 * 字符串其本质是一个字符数组 * * 构造方法: * String(String original):把字符串数据封装成字符串对象 * ...
- Python Request-学习笔记(1)
#导入Requests模块:import requests # 然后,尝试获取某个网页.返回的是reaponse对象,可以从这个对象中获取所有我们想要的信息.response = requests.g ...
- python如何操作excel 基础代码
一 基础操作1打开excel表格并获取表格名称 wookbook = load_workbook(filename = 表格文件路径) (注意只能打开存在的表格,不能用该方法创建一个新表格文件) wo ...
- java解惑之常常忘记的事
java解惑之常常忘记的事 2012-10-17 18:38:57| 分类: JAVA | 标签:基础知识 软件开发 |举报|字号 订阅 针对刚接触java的菜鸟来说,java基础知识 ...
- Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析
Spring3.2 中 Bean 定义之基于 XML 配置方式的源码解析 本文简要介绍了基于 Spring 的 web project 的启动流程,详细分析了 Spring 框架将开发人员基于 XML ...