Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索
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
Output
Sample Input
5 17
Sample Output
4
Hint
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 100010
int n,k,vis[maxn];
struct node{
int x,step;
};
void bfs(){
node p;
p.x = n,p.step = ;
vis[n] = ;
queue<node> q;
q.push(p);
while(!q.empty()){
node tmp = q.front();
q.pop();
if(tmp.x == k){
cout << tmp.step << endl;
return;
}
for(int i=;i<;i++){
int xx;
if(i == ){
xx = tmp.x + ;
}
else if(i == ){
xx = tmp.x - ;
}
else xx = tmp.x * ;
if(xx < || xx > maxn){
continue;
}
if(!vis[xx]){
vis[xx] = ;//注意vis数组
node tp;//这里新建一个结构体对象,不要在原来的tmp上加减!!! 在这里调了一个半小时!!!
tp.x = xx;
tp.step = tmp.step + ;
q.push(tp);
}
}
}
}
int main(){
while(cin >> n >> k){
memset(vis,,sizeof(vis));
if(n<k){
bfs();
}
else{
cout << n - k << endl;
}
}
return ;
}
Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索的更多相关文章
- 迷宫问题 POJ - 3984 [kuangbin带你飞]专题一 简单搜索
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...
- 棋盘问题 POJ - 1321 [kuangbin带你飞]专题一 简单搜索
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- [kuangbin带你飞]专题一 简单搜索(回顾)
A - 棋盘问题 POJ - 1321 注意条件:不能每放一个棋子,就标记一行和一列,我们直接枚举每一行就可以了. AC代码: #include<iostream> #include< ...
- [kuangbin带你飞]专题一 简单搜索
ID Origin Title 454 / 1008 Problem A POJ 1321 棋盘问题 328 / 854 Problem B POJ 2251 Dungeon Ma ...
- [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow
#include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...
- [kuangbin带你飞]专题一 简单搜索 题解报告
又重头开始刷kuangbin,有些题用了和以前不一样的思路解决.全部题解如下 点击每道题的标题即可跳转至VJ题目页面. A-棋盘问题 棋子不能摆在相同行和相同列,所以我们可以依此枚举每一行,然后标记每 ...
- [kuangbin带你飞]专题一 简单搜索 x
A - 棋盘问题 POJ - 1321 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋 ...
- [kuangbin带你飞]专题一 简单搜索 - E - Find The Multiple
//Memory Time //2236K 32MS #include<iostream> using namespace std; ]; //保存每次mod n的余数 //由于198的余 ...
随机推荐
- 在表格中添加text便加框
private void createTableText(Table table) { TableEditor editor = new TableEditor(table); for (int i ...
- restapi(3)- MongoDBEngine : MongoDB Scala编程工具库
最近刚好有同事在学习MongoDB,我们讨论过MongoDB应该置于服务器端然后通过web-service为客户端提供数据的上传下载服务.我们可以用上节讨论的respapi框架来实现针对MongoDB ...
- Python中input()的使用方法
input()以字符串的方式获取用户输入: >>> x = input() 4.5 >>> type(x) <class 'str'> >> ...
- Java网络编程 -- 网络协议
OSI网络七层协议 为使不同计算机厂家的计算机能够互相通信,以便在更大的范围内建立计算机网络,有必要建立一个国际范围的网络体系结构标准.OSI网络七层协议就是在这个基础上制定出来的,其从最底层开始依次 ...
- java优雅注释原则和代码格式列举
一.java的三种注释类型 单行注释:// ...... 块注释:/* ...... */ 文档注释:/** ...... */ 二.指导原则 注释不能美化糟糕的代码,碰到糟糕的代码就重新写吧. 用代 ...
- 讲解开源项目:5分钟搭建私人Java博客系统
本文适合刚学习完 Java 语言基础的人群,跟着本文可了解和运行 Tale 项目.示例均在 Windows 操作系统下演示 本文作者:HelloGitHub-秦人 HelloGitHub 推出的< ...
- 就当我在扯淡,宇宙的bug
Geohot说到“我打算建立一个组织让人们从人工智能模拟中‘越狱’,释放真正的人性.” 不知从何时开始,世界上的知名科学家,黑客等都开始怀疑我们所处世界的真实性. 我们的世界上是真实存在的吗?是否存在 ...
- 数据库炸了——是谁动了我的wait_timeout
1.起因 隐约听到坐在我对面的测试说测试环境的接口有问题 他们一番商讨后,朝我这边反馈说,现在测试环境的接口报504 我条件反射的回了句那是接口超时,再多试几次(测试环境的性能比较差,尤其是数据库,经 ...
- keras 学习-线性回归
园子里头看到了一些最基础的 keras 入门指导, 用一层网络,可以训练一个简单的线性回归模型. 自己学习了一下,按照教程走下来,结果不尽如人意,下面是具体的过程. 第一步: 生成随机数据,绘出散点图 ...
- 在centos6系列vps装Tomcat8.0
In the following tutorial you will learn how to install and set-up Apache Tomcat 8 on your CentOS 6 ...