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 ...
随机推荐
- clang编译mysql(Ubuntu10 64位)
编译安装mysql(Ubuntu10 64位) http://www.cnblogs.com/2018/p/3482259.html 这个介绍了gcc形式编译安装mysql 那我们使用clang进行编 ...
- iOS指南针
前言: 这个小项目使用到了CoreLocation框架里面的设备朝向功能,对CoreLocation感兴趣的可以翻一下之前的文章 在另一个博客站有朋友发现一个尴尬的问题(图片的东西2个方向是不对的), ...
- Android Studio教程--从Github 下载一个Repository
从电脑上彻底删除HelloWorld项目 回到as欢迎页面 复制github中项目的地址 点击clone
- 【代码笔记】iOS-提醒时间的选择
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- SVProgressHUD进度条蒙版
GitHub:https://github.com/samvermette/SVProgressHUDSVProgressHUD和MBProgressHUD效果差不多,不过不需要使用协议,同时也不需要 ...
- 项目管理之道--纪我的新书《PMP项目管理认证学习指南(第4版)》出版并预祝大卖!
新年伊始,我最新的项目管理书籍——<PMP项目管理认证学习指南(第4版)>也出版了,真是新年新气象啊!翻译英文书籍是一件任重道远的工作,除了要具备扎实的基本功,熟悉相关的领域外,还需要细致 ...
- 数据库 SQL语句小结(更新中)
################ Navicat,单条执行sql ################ Navicat,数据库管理工具, 在查询的页面有好多命令,若单条执行: 1:可选中要执行的一条sql ...
- 搬家至独立博客 http://blog.imzjy.com
欢迎访问 http://blog.imzjy.com
- 数据表格,查询、导出共用一个form表单,实现文件流方式下载
在开发中遇到问题是这样的: 在维护老的管理系统的过程中,老板说让加导出功能:项目中,查询的筛选条件是用的表单提交的方式写的. 解决方案有两种: 一.用ajax方式导出 var array = $('# ...
- 多年前写的文本框扩展控件(有ValueChanging事件等),已放github
本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 阅读目录 介绍 起因 代码 使用 GitHub ...