杭电 HDU 2717 Catch That Cow
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8999 Accepted Submission(s): 2837
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?
5 17
4HintThe 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<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
struct Node
{
int x,step;
Node(){}
Node(int x,int step):x(x),step(step){}
}; int n,k;
bool vis[199999]; int bfs()
{
queue<Node>Q;
Node cur,next;
Q.push(Node(n,0));
while(!Q.empty())
{
cur=Q.front();
Q.pop();
for(int i=0;i<3;i++)
{
if(i==0)
next.x=cur.x+1;
if(i==1)
next.x=cur.x-1;
if(i==2)
next.x=cur.x*2;
next.step=cur.step+1;
if(next.x==k)
return next.step;
if(next.x<0||next.x>100000)
continue;
if(!vis[next.x])
{
vis[next.x]=true;
Q.push(next);
}
}
} } int main()
{
while(cin>>n>>k)
{
memset(vis,0,sizeof(vis));
if(n<k)
cout<<bfs()<<endl;
if(n==k)
cout<<0<<endl;
if(n>k)
cout<<n-k<<endl;
}
return 0;
}
杭电 HDU 2717 Catch That Cow的更多相关文章
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- HDU 2717 Catch That Cow(常规bfs)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...
- HDU 2717 Catch That Cow(BFS)
Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 2717 Catch That Cow(广搜bfs)
题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...
- HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............
看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...
- HDU 2717 Catch That Cow (深搜)
题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...
- 题解报告:hdu 2717 Catch That Cow(bfs)
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
随机推荐
- SPI总线介绍
1. 简介 SPI, Serial Peripheral Interface, 串行外设接口, 是一种高速的.全双工.同步的通信总线SPI在芯片的管脚上只占用四根线 SPI接口主要用于MCU与各种外围 ...
- Appium+python自动化5-Appium Inspector【转载】
前言 appium Inspector从入门到放弃!反正你都打开了,那就看下为什么要放弃吧! Appium Inspector是appium自带的一个元素定位工具,上一篇介绍了如何使用uiaut ...
- PHP源码加密- php-beast
php-beast 详细介绍 使用案例: http://www.beastcoder.com/ PHP Beast是一个源码加密模块,使用这个模块可以把PHP源码加密并在此模块下运行. 为什么要用PH ...
- laravel-u-editor工具栏语言切换的方法
更改/config/app.php/locale,可支持en,zh_CN,zh_TW,我们一般设为zh_CN
- AC日记——Array Queries codeforces 797e
797E - Array Queries 思路: 分段处理: 当k小于根号n时记忆化搜索: 否则暴力: 来,上代码: #include <cmath> #include <cstdi ...
- POJ 3264.Balanced Lineup-RMQ(ST)详解
先写一道水题的博客,为后面要写的博客做一个铺垫. ヾ(◍°∇°◍)ノ゙ RMQ(Range Minimum/Maximum Query),即区间最值查询,对于长度为n的数列A,回答若干询问RMQ(A, ...
- (6)java基础知识-基本数据类型、数据类型转换
一.基本数据类型 基本的数据类型一共有四类八种 1.整型 byte: 1字节 取值范围 -128~127 short: 2字节 取值范围 -32768~32767 int: 4字节 取 ...
- (22)C#windows打包部署
程序做好后需要打包部署后才能使用 一.创建安装项目 解决方案右键-添加-新建项目-其他项目类型-安装和部署-双击进入文件系统. 二.制作安装程序 一个完整的安装程序包括项目输出文件.内容文件.桌面快捷 ...
- HttpRunner 接口自动化测试进阶
前面说到了httprunner的安装与简单使用,参见: https://www.cnblogs.com/chengtch/p/8735160.html 这里我们介绍一下通过调试源码的方式来做接口测试: ...
- hiho一下第130周 后缀自动机二·重复旋律7
后缀自动机四·重复旋律7 时间限制:15000ms 单点时限:3000ms 内存限制:512MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 神奇的 ...