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,回溯。的更多相关文章

  1. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  3. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  4. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  5. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  6. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  7. 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 ...

  8. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

  9. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

随机推荐

  1. git 查看当前与上一次version的差异

    http://stackoverflow.com/questions/9903541/finding-diff-between-current-and-last-versions up vote47d ...

  2. position的absolute与fixed共同点与不同点

    这个问题面试被问到过~~ A:共同点: 1.改变行内元素的呈现方式,display被置为block: 2.让元素脱离普通流,不占据空间: 3.默认会覆盖到非定位元素上   B不同点: absolute ...

  3. json对象的简单介绍

    1.JSON(JavaScript Object Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScript原生格式,这意味着在JavaScript中处理JSON数据不需要任 ...

  4. php array_walk_recursive函数的使用

    <?phpfunction myfunction($value,$key) {echo "The key $key has the value $value<br />&q ...

  5. linux系统tomcat项目部署和tomcat访问日志

    一.只用ip地址访问 先把端口号改成80,然后用 <Host name="localhost"  appBase="webapps"    137     ...

  6. ajax遇到的问题

    今天做了个小小的实验,用ajax XMLHttpRequest对象读取服务器上的txt文件里的内容,展示出来 直接把html文件放在桌面用浏览器打开,没有反应,部分代码如下: function oHt ...

  7. java 方法的重载的语法规则

    class People { float hello(int a,int b) { return a+b; } float hello(long a,int b) { return a-b; } do ...

  8. 测试redis+keepalived实现简单的主备切换【转载】

    转自: 测试redis+keepalived实现简单的主备切换 - Try My Best 尽力而为 - ITeye技术网站http://raising.iteye.com/blog/2311757 ...

  9. Centos yum 安装mysql报错 No package mysql-server available.

    这是因为大多数mysql-*的资源名称被mariadb-*重命名了 所以换成 yum install mariadb-server 就可以了 PS[摘自网络] MariaDB不仅仅是Mysql的一个替 ...

  10. stock 当天盘势

    看盘 (一)怎样看大盘当天的指数收阴.收阳 投资者每天都看大盘,但多数人看不准大盘.大盘到底当天是收阴,还是收阳?我通过很长时间的记录.验证,发现大盘在正常时间内,收阴.收阳是有规律的.我判断的准确率 ...