POJ 3278 Catch That Cow(求助大佬)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 109702 | Accepted: 34255 |
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
Output
Sample Input
5 17
Sample Output
4
Hint
Source
void bfs(){
while(!que.empty()) que.pop();
nond tmp;tmp.pos=x;tmp.step=;
vis[x]=;que.push(tmp);
while(!que.empty()){
nond now=que.front();
que.pop();nond a,b,c;
c.pos=now.pos+;c.step=now.step+;if(now.pos<=y&&!vis[c.pos]) que.push(c),vis[c.pos]=;
if(c.pos==y){ printf("%d\n",c.step);return ; }
a.pos=now.pos-;a.step=now.step+;if(now.pos>=&&!vis[a.pos]) que.push(a),vis[a.pos]=;
if(a.pos==y){ printf("%d\n",a.step);return ; }
b.pos=now.pos*;b.step=now.step+;if(now.pos<=y&&!vis[b.pos]) que.push(b),vis[b.pos]=;
if(b.pos==y){ printf("%d\n",b.step);return ; }
}
}
像上面那样写就不行,改成下面这样就AC了。
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int x,y;
int vis[];
struct nond{ int pos,step; };
queue<nond>que;
void bfs(){
while(!que.empty()) que.pop();
nond tmp;tmp.pos=x;tmp.step=;
vis[x]=;que.push(tmp);
while(!que.empty()){
nond now=que.front();
que.pop();nond a,b,c;
if(now.pos==y){ printf("%d\n",now.step);return ; }
c.pos=now.pos+;c.step=now.step+;if(now.pos<=y&&!vis[c.pos]) que.push(c),vis[c.pos]=;
a.pos=now.pos-;a.step=now.step+;if(now.pos>=&&!vis[a.pos]) que.push(a),vis[a.pos]=;
b.pos=now.pos*;b.step=now.step+;if(now.pos<=y&&!vis[b.pos]) que.push(b),vis[b.pos]=;
}
}
int main(){
while(scanf("%d%d",&x,&y)!=EOF){
memset(vis,,sizeof(vis));
bfs();
}
}
POJ 3278 Catch That Cow(求助大佬)的更多相关文章
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- 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 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
随机推荐
- T4模板使用记录,生成Model、Service、Repository
自己目前在搭建一个.NET Core的框架,本来是打算使用前端做代码生成器直接生成到文件的,快做好了.感觉好像使用T4更方便一些,所以也就有了这篇文章~ 我还是有个问题没解决,就是我想生成每个类(接 ...
- 解决:xxx is not in the sudoers file.This incident will be reported.的解决方法
Linux中普通用户用sudo执行命令时报”xxx is not in the sudoers file.This incident will be reported”错误,解决方法就是在/etc/s ...
- 安卓UI自适应性
出于安卓的碎片化原因,针对不同屏幕大小,最好是做到以下几点: 1.能用相对布局的就不用绝对布局,尽量使用权重,weight设置,相对布局和线性布局同条件情况下,优先选线性布局 2.在res目录下创建不 ...
- BZOJ DZY Loves Math系列
⑤(BZOJ 3560) $\Sigma_{i_1|a_1}\Sigma_{i_2|a_2}\Sigma_{i_3|a_3}\Sigma_{i_4|a_4}...\Sigma_{i_n|a_n}\ph ...
- mysql数据库存储的引擎和数据类型
一.查看支持的存储引擎 SHOW ENGINES \G; 或者 SHOW VARIABLES LIKE 'have%'; 二.安装版mysql的默认引擎是InnoDB,免安装版默认引擎是MyISAM ...
- Android项目实战_手机安全卫士手机防盗界面
#安全卫士手机防盗# ###1.Activity的任务栈 1.类似一个木桶,每层只能放一个木块,我们放入木块和取出木块的时候只能从最上面开始操作 ###2.Android中的坐标系
1.秒钟转360度需要60s分60步 2.分针转360度需要3600s分60步 3.秒钟转360度需要43200s分60步 <!DOCTYPE html> <html lang=&q ...
- java攻城狮之路--复习JDBC
1.JDBC中如何获取数据库链接Connection? Driver 是一个接口: 数据库厂商必须提供实现的接口. 能从其中获取数据库连接. 可以通过 Driver 的实现类对象获取数据库连接. 1. ...
- UGUI世界坐标转换为UI本地坐标(游戏Hud的实现)
实现世界坐标的原理是: 世界坐标和UGUI的坐标分属两个坐标系,他们之间是无法进行转换的,需要通过屏幕坐标系来进行转换(因为屏幕坐标是固定的),即先将游戏场景中的世界坐标通过游戏场景Camera转化为 ...
- do{}while(0)
有时会在源码中或在写代码时在宏定义中用到do...while(0). 采用这种方式进行宏定义, 主要是为了防止出现以下错误 : do{}while(0) 空的宏定义避免出现warnning. #def ...