POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 97563 | Accepted: 30638 |
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
题解:
一开始以为是数学找规律,但是看到数据范围很小,n<=1e5,可以用数组存;而且题目要求的是“最少步数”,而“最少步数”经常都是用BFS求的。再将BFS的思想带入看看,发现可以解决问题。
在第一次提交时,RUNTIME ERROR了。但是再看看代码,数组没有开小,不是数组的问题。后来发现再判断某个位置是否vis时,先判断了是否vis,然后再判断这个位置是否合法,这样会导致数组溢出,所以问题就出现在这里了,需谨慎!!
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; int vis[MAXN]; struct node
{
int val, step;
}; queue<node>que;
int bfs(int n, int k)
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.val = n;
now.step = ;
vis[n] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if(now.val==k)
return now.step; tmp.step = now.step+;
if(now.val+>= && now.val+<=1e5 && !vis[now.val+] ) //先判断范围再判断vis !!!
vis[now.val+] = , tmp.val = now.val+, que.push(tmp);
if(now.val->= && now.val-<=1e5 && !vis[now.val-] )
vis[now.val-] = , tmp.val = now.val-, que.push(tmp);
if(now.val*>= && now.val*<=1e5 && !vis[now.val*] )
vis[now.val*] = , tmp.val = now.val*, que.push(tmp);
}
} int main()
{
int n, k;
scanf("%d%d",&n, &k);
cout<< bfs(n,k) <<endl;
}
POJ3278 Catch That Cow —— BFS的更多相关文章
- 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)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- 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: 88732 Accepted: 27795 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
随机推荐
- 洛谷P2365 任务安排 [解法一]
题目描述 N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始 ...
- 2017 ACM/ICPC Asia Regional Urumuqi Online 记录
比赛题目链接 Urumuqi
- Codeforces 515E Drazil and Park (ST表)
题目链接 Drazil and Park 中文题面 传送门 如果他选择了x和y,那么他消耗的能量为dx + dx + 1 + ... + dy - 1 + 2 * (hx + hy). 把这个式子写成 ...
- Centos7安装完成后一些基本操作
1.基本操作一:主机名 # centos7有一个新的修改主机名的命令hostnamectl hostnamectl set-hostname --static www.node1.com # 有些命令 ...
- [BOI2007] Mokia
题目描述 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它能够回 ...
- express---express-session axios
express---express-session axios 使用axios访问后台获取session中的属性值为undefined 在main.js中导入axios import axios fr ...
- Linux下使用nohup实现在后台运行程序(转)
相比上一篇http://www.cnblogs.com/EasonJim/p/6833417.html使用screen实现后台运行程序,各有各的好处,多一种选择吧. Linux下一般比如想让某个程序在 ...
- easyui combobox模糊查询
用easyui框架开发的攻城狮恐怕都遇到过这样一个问题,就是在新增页面combobox下拉框需要支持模糊查询,但是输入不是combobox中Data里面的值的时候,点击保存,依然是可以新增进去的,这样 ...
- 学习日记之抽象工厂模式和Effective C++
抽象工厂模式(Abstract Factory):提供一个创建一系列相关或者相互依赖对象的接口.而无需制定他们详细的类. (1),工厂方法模式是定义一个用于创建对象的接口.让子类决定实例化哪一个类. ...
- iOS 合并.a文件,制作通用静态库
lipo -create SQY/iOS/iphoneos/libGamePlusAPI.a SQY/iOS/iphonesimulator/libGamePlusAPI.a - output ...