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. block 的演练和使用

    概念 block 是 C 语言的 是一种数据类型,可以当作参数传递 是一组预先准备好的代码,在需要的时候执行 动画 block 回顾 self.demoView.center = CGPointMak ...

  2. ANT 配置和安装 1

    --"JAVA_HOME" (JDK HOME)C:\Program Files\Java\jdk1.7.0_79 -- "ANT_HOME" (ANT HOM ...

  3. Core Data & MagicalRecord

    iOS 本地数据持久化存储: 1.plist 2.归档 3.NSUserDefaults 4.NSFileManager 5.数据库 一.CoreData概述 CoreData是苹果自带的管理数据库的 ...

  4. 获取spring bean的utils

    <span style="font-size:10px;">package com.record.util; import org.springframework.be ...

  5. TM3、4波段GeoTiff数据计算NDVI

      源码: 1: PRO TIFF_NDVI,F1,F2,FOUT 2: F1 = DIALOG_PICKFILE(TITLE = 'B4 TIFF',FILTER='*.TIF',/READ) 3: ...

  6. APK反编译(Linux环境下)

    先下载dex2jar和jd-gui这两个软件,然后解压APK,把解压出来的classes.dex,放到dex2jar的根目录,然后命令行进入到dex2jar根目录,然后执行命令   ./dex2jar ...

  7. C#入门经典第九章定义类-1

    1.C#中使用class关键字来定义类 class MyClass { //代码部分 } 定义了类之后,就可以在项目中能访问该定义的其他位置对该类进行实例化.默认情况下,类的声明是内部的,即只有当前项 ...

  8. expdp.sh and impdp.sh

    ####expdp.sh ###### ------------- --   UAT   -- ------------- @D:\dba\change\UAT\eais\env\env_eaisua ...

  9. gridview中button事件处理

    http://msdn.microsoft.com/zh-cn/library/bb907626.aspx 再结合如下: protected void GridView1_RowCommand(obj ...

  10. 我的第一个Android项目之环境搭建

    开发IDE Android Studio2.0 + Genymotion + JDK1.8 网盘地址:http://pan.baidu.com/s/1kUSVqaN Android Studio 我的 ...