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 ...
随机推荐
- [Swift通天遁地]七、数据与安全-(19)使用Swift实现原生的SHA1加密
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- SpringBoot集成CAS单点登录,SSO单点登录,CAS单点登录(视频资料分享篇)
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 很早期的公司 ...
- int(3)和int(11)区别
- ACM_绝对值排序
Why Males And Females Apart? Time Limit: 2000/1000ms (Java/Others) Problem Description: In so many o ...
- Unity学习-工具准备(一)
工具介绍 Unity 4.5.4 VS2013 Visual Studio 2013 Tools for Unity unity3d圣典 五大面板 Hierarchy:场景资源面板 [管理 当前场景 ...
- CUDA-GPU编程
参考:http://blog.csdn.net/augusdi/article/details/12833235 第二节 新建NVIDIA项目: 新建项目及会生成一个简单的代码demo,计算矩阵的加 ...
- Ajax——php基础知识(一)
AMP环境 AMP(Apache.MySQL.PHP)是三个独立的软件,但是对于初学者而言分别安装以及配置需要掌握一定的软件知识,所以就有了很多AMP集成环境帮助我们简化安装 ——WAMP WAMP安 ...
- Java_Web三大框架之Hibernate+jsp+selvect+HQL注册用户
Hibernate比SQL语句简单多了,代码冗余少,切方便简洁明了.下面用Hibernate+jsp+selvect+HQL来实现注册用户. 第一步:编写用户实体类和Users2.hbm.xml映射. ...
- (二)Python 学习第二天--爬5068动漫图库小案例
(注:代码和网站仅仅是学习用途,非营利行为,源代码参考网上大神代码,仅仅用来学习
- Centos7 Openstack - (第二节)添加认证服务(Keystone)
Centos7 install Openstack - (第二节)添加认证服务(Keystone) 我的blog地址:http://www.cnblogs.com/caoguo 根据openstack ...