catch that cow POJ 3278 搜索

题意

原题链接

john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个单位,3. 移动到当前位置的二倍处。输出移动的最少次数。

解题思路

使用搜索,准确地说是广搜,要记得到达的位置要进行标记,还有就是减枝。

详情见代码实现。

代码实现

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7; //这个是数轴的最大尺度
int n, k, ans=inf;
struct node{
int x, t;
};
map<int, int> mp; //使用map进行标记
queue<node> q;
void bfs(int x)
{
mp.clear();
int tx;
node h={x, 0};
q.push(h);
mp[x]=1; //起点也要进行标记
while(!q.empty() )
{
h=q.front();
q.pop();
tx=h.x+1;
if(mp[tx]==0)
{
if(tx == k)
{
ans=min(ans, h.t+1);
return ;
}
//只有当john的位置小于牛的位置时,进行加一操作才有意义
else if(tx < k && tx<maxn)
{
node tmp={tx, h.t+1};
q.push(tmp);
mp[tx]=1;
}
}
tx=h.x-1;
if(mp[tx]==0)
{
if(tx == k)
{
ans=min(ans, h.t+1);
return ;
}
else if(tx >= 0)//减一操作后要判断是不是小于0
{
node tmp={tx, h.t+1};
q.push(tmp);
mp[tx]=1;
}
}
tx=h.x<<1;
if(mp[tx]==0)
{
if(tx == k)
{
ans=min(ans, h.t+1);
return ;
}
else if( h.x < k && tx<maxn) //只有起点小于k时,乘2操作开可以进行
{
node tmp={tx, h.t+1};
q.push(tmp);
mp[tx]=1;
}
}
}
}
int main()
{
scanf("%d%d", &n, &k);
if(n==k)
ans=0;
else bfs(n);
printf("%d\n", ans);
return 0;
}

catch that cow POJ 3278 搜索的更多相关文章

  1. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  2. kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278

    题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...

  3. (广搜)Catch That Cow -- poj -- 3278

    链接: http://poj.org/problem?id=3278 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6211 ...

  4. Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...

  5. C - Catch That Cow POJ - 3278

    //标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...

  6. poj 3278 搜索

    描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediate ...

  7. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

  8. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  9. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

随机推荐

  1. express中app和router的区别

      var app = express(); var router = express.Router(); 以上二者的区别是什么,什么时候用哪个最合适? 区别看下面的例子: app.js var ex ...

  2. 镜像源操作-ananconda-docker

    CentOS mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup CentOS 6 wget - ...

  3. 一些 sql 调优的总结

    一.sql 优化方案 1)列类型尽量定义成数值类型,且长度尽可能短,如主键和外键,类型字段等等   2)建立单列索引   3)根据需要建立多列联合索引.当单个列过滤之后还有很多数据,那么索引的效率将会 ...

  4. 【Leetcode】国王挖金矿

    参考该文章 https://www.cnblogs.com/henuliulei/p/10041737.html #include <iostream> #include <cstr ...

  5. 苹果CMSv10宝塔全自动定时采集教程

    伙伴们在建立好自己的网站添加自定义资源库后,由于手动采集方式比较耗时间和精力更新也不够及时,是不是特别希望能有一个全自动定时采集方法来帮助网站增加视频资源解放自己的双手,那么现在就教大家如何用宝塔一步 ...

  6. vue几种简单的传值方式

    除了一下的几种方式外,可以参考 https://www.cnblogs.com/hpx2020/p/10936279.html 组件传值的方法: 一.父组件向子组件传递数据(props) 第1:父组件 ...

  7. vue 在移动端实现红包雨 (兼容性好)

    下面是代码:<template>    <div class="ser_home">        <ul class="red_packe ...

  8. socket的补充

  9. CentOS7--删除virbr0

    https://blog.csdn.net/aienjoy/article/details/78994128

  10. maven 成长之路

    1配置maven 环境变量 新建系统变量 M2_HOME :E:\apache-maven-3.5.2 在系统变量 path中添加 E:\apache-maven-3.5.2\bin 运行 mvn - ...