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. 怎样将shp文件的坐标点导出来?

    方法一: 1.将线矢量转化为点矢量 具体操作步骤如下: (1)arctoolbox\Data Management tools\Features\双击Feature Vertices to point ...

  2. mvn 本地jar包 加入自己的maven仓库

    -Dfile :你的jar的名称 -DgroupId :在pom中的groupId -DartifactId :在pom中的artifactId -Dversion :在pom中的version 在j ...

  3. Send Email

    private string SendEmail(string mailTo, string body, ref int sendresult) { string errorEmailAddress ...

  4. SQL 行转列(列的值不规则的数目)

    --创建一个临时表用来存储数据 create table #tmp_SNValue_Table (FieldName nvarchar(20), [Value] nvarchar(max)) inse ...

  5. collection:指定要遍历的集合

    //查询员工id'在给定集合中(1,6)的 public List<Employee> getEmpsByConditionForeach(@Param("ids")L ...

  6. 网站模板-AdminLTE:AdminLTE

    ylbtech-网站模板-AdminLTE:AdminLTE 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1. https://adminlte.io/ 1. ...

  7. sqlalchemy.exc.InvalidRequestError: Table 'run_result' is already defined for this MetaData instance

    临时解决办法: 在models文件导入db后,加上如下代码: db.metadata.clear() 但解决问题的根本之处还是在于找到,为何会声明了2次类的定义呢? 解析: table 'roles_ ...

  8. linux(centOS7)的基本操作(二) 目录和文件管理

    1.显示当前工作目录的绝对路径 pwd 2.显示当前工作目录下的子目录和文件 ls [-l] [-h] [-a] 如果只调用ls,子目录和文件会简单的罗列出来,-l表示将其以详细列表的形式展示,-h表 ...

  9. Linux_系统进程管理

    目录 目录 进程管理 进程管理的指令 查看进程ps指令 pgreppidof指令查pid lsof查看系统中的进程 nice指令修改进程的nice值 kill指令结束进程 top系统进程管理器任务管理 ...

  10. tomcat7源码包编译安装

    tomcat/:作用解析jsp程序.先安装jdk容器.1.下载jdk, wget http://download.oracle.com/otn- pub/java/jdk/8u131- b11/d54 ...