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 ...
随机推荐
- 选择符API
querySelector() querySelector()方法接收一个CSS选择符,返回与该模式匹配的第一个元素,如果没有找到匹配的元素,返回null. //获得body元素 var body = ...
- 读取 Excel 之 NPOI
HSSFWorkbook hssfworkbook; void InitializeWorkbook(string path) { //read the template via FileStream ...
- shell 8printf
printf printf使用引用文本或空格分隔的参数,外面可以在printf中使用格式化字符串,还可以制定字符串的宽度.左右对其方式等.printf不会像echo自动添加换行符,因此需要手动添加\n ...
- CentOS 6.6下安装配置Tomcat环境
本文转载至:http://www.linuxidc.com/Linux/2015-08/122234.htm 实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验 ...
- python redis启用线程池管理
pool = redis.ConnectionPool(host=REDIS_HOST, port=REDIS_PORT,max_connections=3,password=REDIS_PASSWO ...
- Spark学习笔记2:RDD编程
通过一个简单的单词计数的例子来开始介绍RDD编程. import org.apache.spark.{SparkConf, SparkContext} object word { def main(a ...
- 【Python编程:从入门到实践】chapter4 操作列表
chapter4 操作列表 4.1 遍历整个列表 magicians=['alice','david','carolina'] for magician in magicians: print(mag ...
- jps命令发生异常
当在集群里输入jps命令时报如下错误: 我就开始检查jdk,感觉应该是centos自动的jdk没卸载干净跟后面安装的jdk冲突 先通过命令 rpm -qa|grep java 查看jdk信息 把这几个 ...
- django-媒体文件,图片存储
1.settings.py # 媒体文件 MEDIA_ROOT = 'media/'
- 最近玩的centos7.3用的防火墙转载
CentOS 7 firewalld使用简介 1.firewalld简介 firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务:第二个就是加入了防火墙的“zone ...