POJ - 3278 Catch That Cow 简单搜索
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
Line 1: Two space-separated integers: N and K
Output
Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
Sample Input
5 17
Sample Output
4
Hint
The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
这道题是关于最短步数的,首先就要想到广搜,既然想到了广搜就可以写代码了。
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0);//Çв»¿ÉÓÃscnaf£»
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e6+10;
const double esp=1e-9;
int m,n,x,y;
int cnt,mini=INF;
bool flag[maxn];
int bfs(int x);
int main()
{
cin>>m>>n;
cout<<bfs(m)<<endl;
}
int bfs(int x)
{
queue<pair<int,int> > t;
t.push(make_pair(x,0));
while(!t.empty())
{
pair<int,int>w=t.front();
if(w.first==n)break;
t.pop();
w.first++,w.second++;
if(w.first<0||w.first>100000||flag[w.first]);
else t.push(w),flag[w.first]=1;
w.first-=2;
if(w.first<0||w.first>100000||flag[w.first]);
else t.push(w),flag[w.first]=1;
w.first++;
w.first*=2;
if(w.first<0||w.first>100000||flag[w.first]);
else t.push(w),flag[w.first]=1;
}
return t.front().second;
}
POJ - 3278 Catch That Cow 简单搜索的更多相关文章
- 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)
题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
随机推荐
- Java第三十二天,IO操作(续集),读写缓冲区的使用
缓冲区出现原因: Java对IO的读写都是经过中间途经--JVM虚拟机进行管理的,JVM调用OS操作系统继续完成IO操作.如此一来,整个过程耗费了不小的资源,为了尽量减少这样重复的周期性操作,出现了缓 ...
- 多级菜单初写(dict使用)
#!/usr/bin/env python3# -*- coding:utf-8 -*-# name:zzyumap = { "中国":{ "北京":{ &qu ...
- 使用Network Emulator Toolkit工具模拟网络丢包测试(下)
用户会在各种网络环境下使用我们的App,PC应用,我们决不能祈求用户的网络环境都是稳定的,因此我们需要模拟出弱网络的情况,用来测试我们的APP在弱网络环境下的表现如何.Network Emulator ...
- 014-预处理指令-C语言笔记
014-预处理指令-C语言笔记 学习目标 1.[掌握]枚举 2.[掌握]typedef关键字 3.[理解]预处理指令 4.[掌握]#define宏定义 5.[掌握]条件编译 6.[掌握]static与 ...
- python填写金数据
''' 将 main 中的{url}改为真 url 将 setd_data中 {姓名} {纬度} {经度} {地址} 改为确切数据 数据自行参考post 结果判断基于响应中是否包含“谢谢参与”字样 ' ...
- MySQL 5.7.18 zip 文件安装过程
安装最新MySQL:5.7.18 1.下载路径 https://dev.mysql.com/downloads/mysql/ 有账号登陆下载, 没有账号:no thanks;just start my ...
- 使用SVG内置API计算图形或点经过transform之后的新坐标
一个应用场景是,点击一条路径,显示该路径的控制点.因为有transform变形( 平移.缩放.倾斜.旋转等变换),所以获取变形后的新坐标需要计算. 纯数学的方法,就是用2D变换矩阵的一些公式去运算,过 ...
- Python 小技之实现的鲜花盛宴,你准备好了吗?
前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:派森酱 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...
- Python的炫技操作:条件语句的七种写法
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: Python极客社区 PS:如有需要Python学习资料的小伙伴可以 ...
- 重装anaconda的记录,包含设置jupyter kernel
anaconda安装记录 官网下载最新版 linux:sh xx.sh 注意不要敲太多回车,容易错过配置bash的部分,还要手动添加 (vim ~/.bashrc 手动添加新bash,卸载时也要删掉此 ...