Hdoj 2717.Catch That Cow 题解
Problem Description
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
HintThe 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.
Source
思路
考验对状态的理解,每次走下一步有3种状态,bfs即可
代码
#include<bits/stdc++.h>
using namespace std;
const int d[3] = {1,-1,0};
struct node
{
int pos;
int step;
};
int n,k;
bool vis[200010];
bool judge(int x)
{
if(vis[x] || x<0 || x>100000)
return false;
return true;
}
int bfs(node st)
{
queue<node> q;
q.push(st);
node next,now;
memset(vis,false,sizeof(vis));
vis[st.pos] = true;
while(!q.empty())
{
now = q.front();
q.pop();
if(now.pos == k) return now.step;
for(int i=0;i<3;i++)
{
if(i==0 || i==1)
next.pos = now.pos + d[i];
else
next.pos = now.pos * 2;
next.step = now.step + 1;
if(judge(next.pos))
{
q.push(next);
vis[next.pos] = true;
}
}
}
}
int main()
{
while(cin>>n>>k)
{
node t;
t.pos = n; t.step = 0;
int ans = bfs(t);
cout << ans << endl;
}
return 0;
}
Hdoj 2717.Catch That Cow 题解的更多相关文章
- hdoj 2717 Catch That Cow【bfs】
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdoj 2717 Catch That Cow
Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...
- 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
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/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 ...
- HUD 2717 Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- 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) ...
随机推荐
- echarts x轴 增加滚动条
charts x轴 增加滚动条 在option 配置项中添加 [ dataZoom 中配置 ] 设置x轴滚动条 效果图: 动态拖动 以下参考代码 dataZoom配置 官网写法 option = { ...
- Python_迭代器、生成器、列表推导式,生成器表达式
1.迭代器 (1)可迭代对象 s1 = ' for i in s1: print(i) 可迭代对象 示例结果: D:\Python36\python.exe "E:/Python/课堂视频/ ...
- Golang中进行reslice时的注意事项
先看下面代码: package main import "fmt" func main() { slice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8} ...
- 【学习总结】Git学习-参考廖雪峰老师教程三-创建版本库
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- 阿里云ECS服务器云监控(cloudmonitor)Go语言版本插件安装卸载与维护
云监控Go语言版本插件安装_主机监控_用户指南_云监控-阿里云https://help.aliyun.com/document_detail/97929.html 云监控cloudmonitor 1. ...
- laravel实现批量添加数据
在使用laravel eloquent进行数据库操作的时候惊讶的发现这货居然不支持批量添加,看到网上很多人在循环里进行数据库插入操作来实现批量添加,我想说这样做是很损失性能滴!好在框架的DB门面里的i ...
- 小程序和H5互调
小程序跳H5页面 https://blog.csdn.net/mytljp/article/details/81030687(copy) H5页面跳小程序 https://blog.csdn.net/ ...
- linux安装php7.2.7
1.下载php 官网下载:#wget http://cn2.php.net/get/php-7.2.7.tar.gz/from/a/mirror.(ps:应该是这么下载的,但是我下载的都是一个mirr ...
- Window上安装—Docker 笔记
本文转自:http://cnodejs.org/topic/55a24267419f1e8a23a64367 需求 想玩nodeClub 源码跑起来,结果window 上各种报错,各种依赖软件要装的感 ...
- Linux基础学习(16)--备份与恢复
第十六章——备份与恢复 一.备份概述 1.Linux系统需要备份的数据: 2.备份策略: 二.dump和restore命令 1.dump命令: 2.restore命令: