poj3278 Catch That Cow
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 73973 | Accepted: 23308 |
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
K
Output
Sample Input
5 17
Sample Output
4
Hint
Source
QAQ,写循环队列wa了好久。。。默默改大队列范围。
可行性剪枝:第一,当前点在牛的左边才进行右移;第二,当前点不能为负数。
15793310
ksq2013 | 3278 | Accepted | 1816K | 32MS | G++ | 1159B | 2016-07-23 19:37:16 |
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,cow;
bool vis[200100];
struct que{
int fmr,stp;
}q[201000];
int bfs()
{
int head=0,tail=1;
q[0].fmr=n;
q[0].stp=0;
vis[n]=1;
while(head^tail){
que now=q[head++];
//if(head==10000)head=0;
if(!(now.fmr^cow))return now.stp;
if(now.fmr-1>=0&&!vis[now.fmr-1]){
vis[now.fmr-1]=1;
q[tail].fmr=now.fmr-1;
q[tail].stp=now.stp+1;
tail++;
//if(tail==10000)tail=0;
}
if(now.fmr<=cow&&!vis[now.fmr+1]){
vis[now.fmr+1]=1;
q[tail].fmr=now.fmr+1;
q[tail].stp=now.stp+1;
tail++;
//if(tail==10000)tail=0;
}
if(now.fmr<=cow&&!vis[now.fmr<<1]){
vis[now.fmr<<1]=1;
q[tail].fmr=now.fmr<<1;
q[tail].stp=now.stp+1;
tail++;
//if(tail==10000)tail=0;
}
}
}
int main()
{
while(~scanf("%d%d",&n,&cow)){
memset(q,0,sizeof(que));
memset(vis,0,sizeof(vis));
printf("%d\n",bfs());
}
return 0;
}
poj3278 Catch That Cow的更多相关文章
- 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 ...
- POJ3278——Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 114140 Accepted: 35715 ...
- POJ3278 Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- poj-3278 catch that cow(搜索题)
题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...
- 抓住那只牛!Catch That Cow POJ-3278 BFS
题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...
- bfs—Catch That Cow—poj3278
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 87152 Accepted: 27344 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
随机推荐
- ArcGis设置到 Oracle 的连接
设置到 Oracle 的连接 地理数据 » 管理地理数据库 » Oracle 中的地理数据库 要建立从客户端计算机到 Oracle 数据库的连接,必须在客户端计算机上安装 Oracle 客户端应用程序 ...
- 关于Android Force Close 出现的原因 以及解决方法
一.原因: forceclose,意为强行关闭,当前应用程序发生了冲突. NullPointExection(空指针),IndexOutOfBoundsException(下标越界),就连Androi ...
- iOS 杂笔-22(万年一遇~一张图片对代理的理解)
iOS 杂笔-22(万年一遇~一张图片对代理的理解) 建议:本博客需要对代理有一定了解方可阅读(反正我也管不到) 图片 在图片之外设置协议(没有这东西这篇博客也就是夭折了) 下面我对图片中出现的形形色 ...
- javascript 创建对象
1.原型模式创建对象 (1)第一种function Newperson(){ } var person2 = new Newperson(); Newperson.prototype.name ...
- Azure File
Azure File 服务使用标准 SMB 2.1 协议提供文件共享.Azure 中运行的应用程序现在可以使用熟悉的标准文件系统 API(如 ReadFile 和 WriteFile)在虚拟机之间轻松 ...
- ORACLE 查看有多个执行计划的SQL语句
在SQL优化过程,有时候需要查看哪些SQL具有多个执行计划(Multiple Executions Plans for the same SQL statement),因为同一个SQL有多个执行计划一 ...
- SQL Server 2012中Task是如何调度的?
SQL Server 2012中Task是如何调度的?[原文来自:How It Works: SQL Server 2012 Database Engine Task Scheduling] ...
- Xtrabackup数据全备份与快速搭建从服务器
Percona Xtrabackup可以说是一个完美的数据备份工具.特别是当数据库的容量达到了一定数量级的时候且存在单表达到几十G的数据量, 很难容忍一些逻辑备份的漫长时间.如单个数据库约200G,单 ...
- sed实例精解--例说sed完整版
原文地址:sed实例精解--例说sed完整版 作者:xiaozhenggang 最近在学习shell,怕学了后面忘了前面的就把学习和实验的过程记录下来了.这里是关于sed的,前面有三四篇分开的,现在都 ...
- C#委托学习
标签(空格分隔): C# 看Markdown效果支持的不大好. 买来<CLR Via C#>这本书很久了,一直也没有对其进行总结,看的非常凌乱,趁此机会好好总结一下,也算对C#学习的一个总 ...