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 ...
随机推荐
- app定位器带xpath的 uiautomatorviewer
参考文档:http://blog.csdn.net/kaka1121/article/details/53301517 在写脚本的过程中,总是遇到标签没有ID或者name等类似的唯一识别属性,为了能识 ...
- 大家都对vertical-align的各说各话
原文地址:http://www.blueidea.com/tech/web/2008/5892.asp 最近几天仔细研究了一下vertical-align这个属性,结果让我大吃一惊,这个很“资深”的C ...
- python logging模块使用教程
简单使用 #!/usr/local/bin/python # -*- coding:utf-8 -*- import logging logging.debug('debug message') lo ...
- python 之路之函数01
一 函数 1 那么要想学习函数,我们首先应该知道函数是什么? 我们这里所说的函数与数学中的函数是不同的概念,在这里我们可以把函数看成我们写代码过程中需要用到的工具. 2 那么这个‘工具’有什么 ...
- javascript 常用获取页面宽高信息 API
在页面的构建中 常常会需要获取页面的一些宽高信息,例如实现 惰性加载图片 需要获取页面的可见区域高度 和 已滚动区域的高度,以判断图片所在位置是否可见来决定加载图片的时间, 花点时间整理了一下,获取页 ...
- ansible的安装与使用
ansible的特点: 1. 基于ssh运行 2. 无需客户端 安装ansible 这里提供四种安装方式,根据自己的需要任选一种即可 1.1使用yum安装 yum install epel-relea ...
- 20165233 2017-2018-2 《Java程序设计》第九周学习总结
20165233 2017-2018-2 <Java程序设计>第九周学习总结 教材学习内容总结 基础 - URL类:java.net包中的URL类是对统一资源定位符的抽象,使用URL创建对 ...
- java 调用apache.commons.codec的包简单实现MD5加密
转自:https://blog.csdn.net/mmd1234520/article/details/70210002/ import java.security.MessageDigest; im ...
- 10.Action中的method属性
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...
- xml 创建 和 处理 及其修改
#创建xml import xml.etree.ElementTree as ET new_xml = ET.Element('namelist') personinfo = ET.SubElemen ...