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 ...
随机推荐
- Autodesk Cloud Accelerator Program 开始报名
如果你没有注意到这个消息,那你就会错过一个前往旧金山和硅谷工程师一起工作数周的机会. 摘要一下: 时间: 1月10前提交你的提案,3月飞往旧金山 地点: 旧金山. 包住宿哦~ 不过,既然要去美国,既然 ...
- 跨越千年的RSA算法
转载自http://www.matrix67.com/blog/archives/5100 数论,数学中的皇冠,最纯粹的数学.早在古希腊时代,人们就开始痴迷地研究数字,沉浸于这个几乎没有任何实用价值的 ...
- Jquery分页功能
Jquery代码 /// <reference path="jquery-1.9.1-vsdoc.js" />//锚点var anchor="#apage&q ...
- Android Activity动画
动画XML文件 slide_right_in.xml <?xml version="1.0" encoding="utf-8"?> <set ...
- Android中的XML解析
在安卓中主要有三种XML文档解析方式:DOM(Document Object Model), SAX(Simple API for XML), PULL 他们的主要特点如下表: 特点 主要类 DO ...
- OC中的字典NSDictionary
========================== 字典 ========================== NSDictionary * dict = [[NSDictionary alloc] ...
- BitTorrent Sync - 神奇的文件同步软件,无需服务器让多台电脑互相同步!
176,487 微博 腾讯 空间 微信 141 49 如今人们对文件备份和同步的需求已经越来越强烈了.像 Dropbox 一样的云存储网盘有很多,但它们都有一个局限性,就是所有的文件都得经过它们的服务 ...
- Android简化xml sax解析
dom解析占用内存大(我这边需要解析各种各样的kml文件,有时4-5M的kml文件使用dom解析很多手机就内存溢出了),也需要引入第三方库,所以使用相对于节省内存很多.不需引入其他库的sax解析就是很 ...
- mysql-mmm 安装配置(双主)
原文地址:mysql-mmm 安装配置 作者:chinaunix1116 MMM即Master-Master Replication Managerfor MySQL(mysql主主复制管理器)关于m ...
- js相对路径相关(比如:js中的路径依赖导入该js文件的路径)
问题描述: 前几天调用同事的js接口文件,在他自己的html测试页面ok,在我这边调用时出现问题. debug过程中,将该测试html移到其他位置都不行,放到原html测试页面同层次路径下是OK的. ...