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;
}
- Node.js新手必须知道的4个JavaScript概念
- 博客文章重新启用评论,附一键填写评论中用户信息代码生成工具
- 提升 Node.js 应用性能的 5 个技巧
- 再次扩散:Linux系统bash漏洞CVE-2014-6271仍未被彻底修复,红帽再发补丁
- SEO分享:彻底禁止搜索引擎收录非首选域名的方法
- 紧急扩散:Linux系统bash严重安全漏洞CVE-2014-6271的检测及修复方法
- 手游热更新方案xLua开源:Unity3D下Lua编程解决方案
- WordPress评论滑动/拉链解锁myQaptcha代码版及部署方法
- 修改WordPress分类目录和页面链接以斜杠/结尾,附nginx对应的301跳转设置方法
- 彻底弄懂 Http 缓存机制 - 基于缓存策略三要素分解法
- 解决Crayon Syntax Highlighter代码高亮与fancybox图片暗箱冲突问题
- 移动搜索SEO:网站移动适配之Meta标注、移动跳转终结篇
- http加速器Varnish的vcl配置小记(lnmp环境)
- 关于网站图标favicon.ico那点事儿,你造吗?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Java Redis系列2 (redis的安装与使用+redis持久化的实现))
- Apache IoTDB 系列教程-1:数据模型
- Java Redis系列3(Jedis的使用+jedis连接池技术)
- kali中安装漏洞靶场Vulhub(超详细)
- [全部章节]栈论 : 递归与栈式访问,如何用栈实现所有递归操作 (内附幼儿园题目,要笑着做完)
- Apache IoTDB 系列教程-2:基础 SQL 操作
- Apache IoTDB 系列教程-3:部署运维
- 栈论 : 递归与栈式访问,如何用栈实现所有递归操作(幼儿园题目篇)
- kali下安装beef并联合Metasploit
- 初次使用maven创建web工程发现只有一个idea目录,src,webapp目录都不见了,解决方案
- 栈论 : 递归与栈式访问,如何用栈实现所有递归操作(幼儿园题目篇,题目2)
- SQL注入原理及代码分析(一)
- SQL注入原理及代码分析(二)
- 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出 现的次数。
- SSRF漏洞简单分析