POJ3287(BFS水题)
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
FJ要抓奶牛。
开始输入N(FJ的位置)K(奶牛的位置)。
FJ有三种移动方法:1、向前走一步,耗时一分钟。
2、向后走一步,耗时一分钟。
3、向前移动到当前位置的两倍N*2,耗时一分钟。
问FJ抓到奶牛的最少时间(奶牛不动)
解题思路:很明显我们求最短的时间就相当于求走的最少步数,所以我们就很容易想到用广搜BFS了,把每次FJ可以到的位置都压入队列,然后用一个vis数组标记FJ是否已经去过该位置了,再用 一个step数组存储FJ到该位置所需要的最短时间即可。要注意的是,他的位置也有范围是0-100000,开始我就是因为右区间少了个=号,WA了好几次,所以还是要细心。难的做不出只好做简单的了。。。
附上代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int vis[]; //标记是否走过
int step[]; //储存到该处的最短时间
int n,k;
queue<int> que;
int ans; int BFS()
{
que.push(n);
step[n]=;
vis[n]=;
while(que.size())
{
int p=que.front();
que.pop();
if(p==k) break;
for(int i=;i<;i++)
{
int dx;
if(i==) dx=p-;
if(i==) dx=p+;
if(i==) dx=*p;
if(dx>=&&dx<=&&vis[dx]==)
{
que.push(dx);
step[dx]=step[p]+;
vis[dx]=;
} }
}
return step[k];
} int main()
{
while(cin>>n>>k)
{
memset(vis,,sizeof(vis));
memset(step,,sizeof(step));
ans=BFS();
cout<<ans<<endl;
}
return ;
}
POJ3287(BFS水题)的更多相关文章
- POJ 3984 - 迷宫问题 - [BFS水题]
题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...
- hdu1240 bfs 水题
原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...
- POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)
http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...
- nyoj--523--亡命逃窜(BFS水题)
亡命逃窜 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧.不过英雄不是这么好当的.这个可怜的娃被魔 ...
- POJ 3126 Prime Path bfs, 水题 难度:0
题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...
- codeforces 811 D. Vladik and Favorite Game(bfs水题)
题目链接:http://codeforces.com/contest/811/problem/D 题意:现在给你一个n*m大小的图,你输出一个方向之后,系统反馈给你一个坐标,表示走完这步之后到的位子, ...
- CYJian的水题大赛
实在没忍住就去打比赛了然后一耗就是一天 最后Rank19还是挺好的(要不是乐多赛不然炸飞),这是唯一一套在Luogu上号称水题大赛的而实际上真的是水题大赛的比赛 好了我们开始看题 T1 八百标兵奔北坡 ...
- hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]
题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
随机推荐
- QQ群管理员申请帖(本次截止日期为2017-03-25)
本帖专门为技术交流群申请管理员专用. 管理员的权利: 1.有权在成员违规的情况下直接剔除. 2.有权加入多个交流群. 3.有权引人入群. 4.艾特全体是权利,但要慎用,通常情况下,没有我本人的授意,不 ...
- Linux基础命令-Nginx-正则表达式( grep sed awk )-Shell Script--etc
Linux基础使用 学习内容博客 内存 查看swap分区信息 > swapon -s 添加swap分区 > mkswap /dev/sdb2 > 激活 swapon -a /dev/ ...
- 撰写POPUSH需求文档
不当家不知柴米贵,撰写了正规的软件需求文档才知道软件工程的复杂性 感谢@洪宇@王需@江林楠下午的加班加点,五个人正闷在406B奋斗中,加油!
- beta版说明书
项目名称:GoGoing 软件使用说明: 在主界面是可以点击选择景点门票区间来选择景点,同时也可搜索景点显示信息. 还可以通过定位功能显示附近景点. 点开门票区间后是一些景点的图片和简介,还可以通过距 ...
- 5.2&5.3
队友吕日荣 http://www.cnblogs.com/Russelling/ 最近队友有点忙,尽管如此,队友还是有很给力的付出,让我们在最后完成了任务. 一开始都不知道这次的任务是要做什么,毫无头 ...
- 使用matlab自带工具实现rcnn
平台:matlab2016b matlab自带一个cifar10Net工具可用于深度学习. 图片标注 这里使用的是matlab自带的工具trainingImageLabeler对图像进行roi的标注. ...
- Maven+SSM整合.doc
Maven + SSM整合 1开发环境搭建 1.1Eclipse4.7(Oxygen) + Tomcat8 + Maven3.5.2 2Maven Web项目创建 2.1新建maven项目 2.2 选 ...
- ERROR 2003: Can't connect to MySQL server on 'host ip'(10060)
https://forums.mysql.com/read.php?51,99347,99358 https://dev.mysql.com/doc/refman/5.7/en/can-not-con ...
- charCodeAt与fromCharCode
charCodeAt() 方法可返回指定位置的字符的 Unicode 编码 这个返回值是 0 - 65535 之间的整数. stringObject.charCodeAt(index) /* a-z ...
- laravel 在nginx服务器上除了首页其余都是404的问题
nginx对应站点的.conf配置文件添加如下代码 location / { try_files $uri $uri/ /index.php$is_args$query_string; #语法: tr ...