(简单) POJ 1278 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?
也是典型的BFS的题目,我第一次设的数组最大是100000*10,然后过了,第二次100000+5,居然也过了。。。
这个题就是从出发点开始bfs,有三条路可以走,一知道发现牛为止。
代码如下:
#include<iostream>
#include<cstring>
#include<queue> using namespace std; int N,K;
int rem[]; void bfs()
{
queue <int> que;
int t; que.push(N);
rem[N]=; while(!que.empty())
{
t=que.front();
que.pop(); if(t==K)
return; if(t*<=&&rem[t*]==-)
{
rem[t*]=rem[t]+;
que.push(t*);
}
if(t+<=&&rem[t+]==-)
{
rem[t+]=rem[t]+;
que.push(t+);
}
if(t->=&&rem[t-]==-)
{
rem[t-]=rem[t]+;
que.push(t-);
}
}
} int main()
{
ios::sync_with_stdio(false); while(cin>>N>>K)
{
memset(rem,-,sizeof(rem)); if(K<=N)
cout<<N-K<<endl;
else
{
bfs();
cout<<rem[K]<<endl;
}
} return ;
}
(简单) POJ 1278 Catch That Cow,回溯。的更多相关文章
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- 【转】RestQL:现代化的 API 开发方式
原文:http://tech.meituan.com/koa-restql.html RestQL:现代化的 API 开发方式 李鑫 ·2016-08-12 11:26 koa-restql 已经在 ...
- 封装sdk API 应用
1 #include "QWinApp.h" 2 #include "QGlobal.h" 3 int WINAPI _tWinMain(HINSTANCE h ...
- HDU5908 Abelian Period 暴力
题目大意:将一个数组分成长度为k的几个连续区间,如果每个区间内各个元素出现的次数相同,则称k为一个阿贝尔周期,从小到大打印所有阿贝尔周期,数据间加空格. 题目思路:map+暴力 #include< ...
- shell 空格问题
1.定义变量时, =号的两边不可以留空格. eg: gender=femal------------right gender =femal-----------wrong gender= femal- ...
- oracle中的常用函数1-------decode方法
DECODE函数是ORACLE PL/SQL是功能强大的函数之一,目前还只有ORACLE公司的SQL提供了此函数,其他数据库厂商的SQL实现还没有此功能.DECODE有什么用途呢? 先构造一个例子,假 ...
- 两个byte[]拼接
//两个byte[]拼接 public byte[] copybyte(byte[] a, byte[] b, byte[] c, byte[] d, byte[] e)///,byte[] f,by ...
- ajax遇到的问题
今天做了个小小的实验,用ajax XMLHttpRequest对象读取服务器上的txt文件里的内容,展示出来 直接把html文件放在桌面用浏览器打开,没有反应,部分代码如下: function oHt ...
- 学习笔记——原型模式Prototype
原型模式,简单说就是具有一个克隆方法,外部可以直接使用此方法得到相应对象的拷贝对象. 比如哆啦A梦的复制镜,一照,就把物品拷贝了一份(虽然是镜子复制是相反的,这里就忽略这个细节了) C++中依靠拷贝构 ...
- ZABBIX自定义用户KEY与参数USERPARAMETERS监控脚本输出
zabbix在模板中预定义了一些key,但通常情况,并不能满足我们的需求.幸运的是zabbix提供了自定义key的方法,因此我们可以灵活的监控各种我们想要监控的数据. 定义配置文件 通过yum安装的z ...
- 利用hibernate实现oracle(每张表的ID)自动增长
设置ID的增长策略是sequence,同时指定sequence的名字,最好每个表建一个sequence 例如创建一个序列:CREATE SEQUENCE DEPARTMENT_ID_SEQ MINVA ...