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 ...
随机推荐
- java web 程序---缓冲代码
在写验证码的时候,我的验证码是随机的,所以每次点击时,刷新页面,验证码都会改变. 可是,当我点击刷新时,验证码不变,说明,没有缓冲. 这里差三行代码. response.setHeader(" ...
- 开发框架:IOE 架构
ylbtech-开发框架:IOE 架构 传统的IOE架构(IBM小型机.EMC存储设备.Oracle数据库). 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 ...
- jquery对象和javascript对象的console.log结果
array.push($("div").children("label")); console.log(array); 输出: 这个是jquery对象,如果在选 ...
- python3 钉钉群机器人 webhook
import requests import json url='https://oapi.dingtalk.com/robot/send?access_token=替换成你自己的toten' pro ...
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1995-10-08','2015-11-12',' ...
- HttpURLConnection连接超时问题
1.问题描述 这几天测试重构后的下载框架,发现在下载过程中如果网络中断或网络较差,个别应用的下载就会阻塞卡住,一直卡在 “正在下载 xx%”. 2.问题排查和定位 思考:网络差不应该报网络异常的错 ...
- Vue 路由配置、动态路由
1.安装 npm install vue-router --save / cnpm install vue-router --save 2.引入并 Vue.use(VueRouter) (main.j ...
- 学习MongoDB 七: MongoDB索引(索引基本操作)(一)
一.简介 在MongoDB建立索引能提高查询效率,只需要扫描索引只存储的这个集合的一小部分,并只把这小部分加载到内存中,效率大大的提高,如果没有建立索引,在查询时,MongoDB必须执行全表扫描,在数 ...
- SQL SERVER回滚恢复误操作的数据
在生产数据库做CURD操作时,可能会有执行某条语句误操作的情况发生,针对这个种情况有两点建议: 1. 在SQL SERVER上开启事务确认功能,当执行完语句后确认无误,再提交事务.(开启方法见附件图片 ...
- 在IDEA下使用Spring Boot的热加载(Hotswap)
你是否遇到过这样的困扰: 当你写完一段代码后,要看到效果,必须点击IDEA的停止按钮,然后再次重启启动项目,你是否觉得这样很烦呢? 如果你觉得很烦,本文就是用来解决你的问题的. 所谓热加载,就是让我们 ...