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 ...
随机推荐
- E20170709-hm
scrape vt. 擦,刮; 擦去; 擦伤,刮破; 挖空;
- yii登陆中添加验证码
1.在SiteController中添加如下代码: /** * Declares class-based actions. */ public function actions() { return ...
- [Swift通天遁地]四、网络和线程-(10)处理图片:压缩、缩放、圆角、CoreImage滤镜、缓存
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]七、数据与安全-(14)使用单元测试进行邮箱格式的验证
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- markdownpad2下载安装教程
1.下载安装 http://markdownpad.com/download/markdownpad2-setup.exe 直接下载,安装过程中提醒要安装微软的一个什么环境,不用理会直接跳过,实测没有 ...
- 对于Mobile模块化的概念认知(小白)
最近刚刚学习了Mobile的一些基础知识,把它整理一下方便自己的学习 那什么是Mobile呢? 自己的理解是将一个项目中共同的部分抽出来,这样就形成了Mobile模块. 为什么要使用Mobile呢? ...
- 【转】Linux下变量内容删除与替换
转自:http://www.linuxidc.com/Linux/2015-01/111781.htm 当一个变量被赋予值后,有时会对变量的值进行一些微小的调整,比如删除变量值中特定一部份,或替换掉一 ...
- 15年用canvas画的
请恕我当年的工作太轻松,用canvas手打了一个图,技术含量并没有什么,现在看看,甚是怀念_(¦3」∠)_ <!DOCTYPE html> <html> <head&g ...
- .net Jquery动态显示当前时间
<span id="Timer"></span> <script type="text/javascript"> $(fun ...
- [Android]异常4-javax.mail.AuthenticationFailedException
javax.mail.AuthenticationFailedException 背景:JavaMail发送电子邮件 异常原因: 可能一>发件人帐号.密码有误 可能二>需要使用SSL安全认 ...