poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 52463 | Accepted: 16451 |
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
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue> using namespace std; #define PI acos(-1.0)
#define EPS 1e-10
#define lll __int64
#define ll long long
#define INF 0x7fffffff queue<pair<int,int> > qu;
bool b[]; inline bool Check(int x);
int Bfs(int r1,int r2); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int r1,r2;
scanf("%d %d",&r1,&r2);
printf("%d\n",Bfs(r1,r2));
return ;
}
inline bool Check(int x){
return x>=&&x<=&&b[x]==;
}
int Bfs(int r1,int r2){
qu.push(make_pair(r1,));
b[r1]=;
while(!qu.empty()){
pair<int,int> tmp=qu.front();
qu.pop();
if(tmp.first==r2){
return tmp.second;
}
int x=tmp.first+;
if(Check(x)){
b[x]=;
qu.push(make_pair(x,tmp.second+));
}
x=tmp.first-;
if(Check(x)){
b[x]=;
qu.push(make_pair(x,tmp.second+));
}
x=tmp.first*;
if(Check(x)){
b[x]=;
qu.push(make_pair(x,tmp.second+));
}
}
}
我自己底下实验了下,把STL去掉,自己实现简易的队列,再进行暴搜,时间消耗为16ms。
代码二:【0ms】bfs剪枝(这里没有用STL): 剪枝内容:当自己的位置大于k的时候,自然不用考虑+1和*2的情况了。
#include <fstream>
#include <iostream>
#include <cstdio> using namespace std; const int N=;
int n,k,dis[N],queue[N];//dis:记录走到某个点的步数。 queue:因为每次队列采用去重加入,所以N是足够用了。
bool b[N];//N是足够的,不需要扩展2N或者3N。打比说x先乘再减,与先减再乘,后者会更快,故不会超过N,负数是更不可能的,因为没有除法。 int bfs(); int main()
{
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
scanf("%d%d",&n,&k);
printf("%d",bfs());
return ;
}
int bfs(){
int l,r,x;
l=r=;
dis[n]=;
queue[]=n;
while(){//队列不可能为空
if(queue[l]==k) return dis[k];
if(x=queue[l]-,x>=&&!b[x]){//注意边界
b[x]=;
queue[++r]=x;
dis[x]=dis[queue[l]]+;
}
if(x=queue[l]+,queue[l]<=k&&!b[x]){//这里的queue[l]<=k为剪枝
b[x]=;
queue[++r]=x;
dis[x]=dis[queue[l]]+;
}
if(x=queue[l]<<,queue[l]<=k&&x<N&&!b[x]){//这里的queue[l]<=k为剪枝;注意边界
b[x]=;
queue[++r]=x;
dis[x]=dis[queue[l]]+;
}
l++;
}
}
poj3278-Catch That Cow 【bfs】的更多相关文章
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- POJ - 3278 Catch That Cow 【BFS】
题目链接 http://poj.org/problem?id=3278 题意 给出两个数字 N K 每次 都可以用三个操作 + 1 - 1 * 2 求 最少的操作次数 使得 N 变成 K 思路 BFS ...
- POJ 3278 Catch That Cow【BFS】
题意:给出n,k,其中n可以加1,可以减1,可以乘以2,问至少通过多少次变化使其变成k 可以先画出样例的部分状态空间树 可以知道搜索到的深度即为所需要的最小的变化次数 下面是学习的代码----@_@ ...
- 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj3278 【BFS】
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 97240 Accepted: 30519 ...
- poj3278 Catch That Cow(简单的一维bfs)
http://poj.org/problem?id=3278 ...
- HDU2717 Catch That Cow 【广搜】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
随机推荐
- springMVC参数绑定JSON类型的数据
需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...
- Appium录制脚本520-2
1.录制自动化脚本 场景:启动雪球,点击我的,登陆雪球,选择手机及其他登陆,输入手机号 2.使用Java进行测试Appium测试 2.1创建Java工程 file-创建maven工程-填写GroupI ...
- JDK1.5多线程提高
1.名词: 1.任务的执行与任务的提交解耦 2.任务的执行策略-可中断,取消 2.线程封闭机制: 针对单线程池而言,提高任务执行的速度,但是无需锁定 3.饥饿死锁: 任务长期得不到执行,其实就是形成闭 ...
- Windows10 命令行中使用网络驱动器
Windows10中,我们在局域网内使用共享文件夹,建立映射的网络驱动器,有时候需要一些软件去调用网络驱动器内的资源,但是发现在资源管理器能正常打开,应用软件却无法识别,命令行中提示:“系统找不到指定 ...
- AVL树Python实现
# coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...
- Python 之 cas-clinet
因为要搞一个用户登录安全的验证,要用到cas服务,所以在网上搜了很多关于cas信息才搞成功. 我写的属于客户端的cas就是从CAS服务,获取返回的ticket验证通过,用户登录成功. 使用的是web. ...
- 外观设计模式 (Facade)
目的:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使 外观设计模式使用场合: 1. 在设计初期阶段,应该有意识的将不同的两个分层.层与层之间建立外观 ...
- position属性详解
内容: 1.position属性介绍 2.position属性分类 3.relative相对定位 4.absolute绝对定位 5.relative和absolute联合使用进行定位 6.fixed固 ...
- 0_Simple__simpleTemplates + 0_Simple__simpleTemplates_nvrtc
使用 C++ 的模板 ▶ 源代码:静态使用 // sharedmem.cuh #ifndef _SHAREDMEM_H_ #define _SHAREDMEM_H_ // SharedMemory 的 ...
- testng报告发邮件后css样式缺失问题
问题:用reportng把代替testng报告后,邮件中不显示html样式 解决方案:把依赖的文件,加到邮件附件 Jenkins发邮件的时候,把依赖文件作为附件发送. 结果看到样式了: