catch that cow POJ 3278 搜索
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 搜索的更多相关文章
- Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
- kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278
题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...
- (广搜)Catch That Cow -- poj -- 3278
链接: http://poj.org/problem?id=3278 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6211 ...
- Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。
题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...
- C - Catch That Cow POJ - 3278
//标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...
- poj 3278 搜索
描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediate ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
随机推荐
- org.hibernate.NonUniqueObjectException 原因及解决办法
问题 使用hibernate更新对象时,出现如下错误: org.hibernate.NonUniqueObjectException: a different object with the same ...
- GO语言学习笔记2-int类型的取值范围
相比于C/C++语言的int类型,GO语言提供了多种int类型可供选择,有int8.int16.int32.int64.int.uint8.uint16.uint32.uint64.uint. 1.i ...
- 最全的WEB前端开发程序员学习清单
史上最全的WEB前端开发程序员学习清单! 今天为什么要给大家分享这篇文章呢,我发现最近来学前端的特别多,群里面整天都有人问:前端好找工作吗?前端要怎么学啊?前端工资怎么样?前端XX,前端XXX,虽然我 ...
- mobx使用
1.mobx状态管理 安装:creact-react-app mobx action.store.reducer. action是一个函数,事件调用actions直接修改state,Actions是唯 ...
- jeesite安装时Perhaps you are running on a JRE rather than a JDK
使用自己本地安装的maven,启动jeesite报错: No compiler is provided in this environment. Perhaps you are running on ...
- postgres select TOP X in group 查询每个组的前几名
参考: https://stackoverflow.com/questions/27415706/postgresql-select-top-three-in-each-group http://ch ...
- [SHOI2005]树的双中心
题目链接:Click here Solution: 首先我们要知道,选择两个点\(A,B\),必定存在一条边,割掉这条边,两个集合分别归\(A,B\)管 再结合题目,我们就得到了一个暴力的\(n^2\ ...
- PCL智能指针疑云 <三> 智能指针作为函数的传值参数和传引用参数
一 函数的参数传递可以简单分类为“传值”和“传引用”. 声明函数时,形参带引用“&”,则函数调用时,是把实参所在的内存直接传给函数所开辟的栈内存.在函数内对形参的修改相当于对实参也进行修改. ...
- 洛谷P4208 [JSOI2008]最小生成树计数——题解
题目传送 前置知识:对于同一个图的所有最小生成树,权值相等的边的数量相同. 可以简单证明一下: 我们可以从kruskal的过程考虑.这个算法把所有边按权值大小从小到大排序,然后按顺序看每条边,只要加上 ...
- Shell test命令/流程控制
Shell test命令 Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值.字符和文件三个方面的测试. 数值测试 参数,说明 -eq等于则为真 -ne不等于则为真 -gt 大于则 ...