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) ...
随机推荐
- java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader
我的解决办法: 1.如果是application工程,则在程序中打印出 system.out.println(System.getProperty("java.endo ...
- MySQL 主从同步遇到的问题及解决方案
在做某个项目的时候,使用主从数据库,master负责update.delete.insert操作,而slave负责select操作. 情景1:发表文章与查看文章 可以认为这个项目是一个博客系统,这里就 ...
- Java 数据库简单操作类
数据库操作类,将所有连接数据库的配置信息以及基本的CRUD操作封装在一个类里,方便项目里使用,将连接数据库的基本信息放在配置文件 "dbinfo.properties" 中,通过类 ...
- [新三板摘牌]国资企业济南华光光电去年终止拟IPO今年摘牌新三板
国资企业济南华光光电去年终止拟IPO今年摘牌新三板 http://blog.sina.com.cn/s/blog_e32cfa770102ycku.html http://stock.qlmoney. ...
- java学习之—栈匹配字符串符号
/** * 栈 * Create by Administrator * 2018/6/11 0011 * 上午 10:20 **/ public class StackR { private int ...
- Protocol buffers--python 实践(二) protocol buffers vs json
为什么专门开一个坑,来使用pb.放弃本在各平台上都支持得很好的json而使用pb的一个归根到底的理由,就是希望在保证强类型和跨平台的情况下,能够更轻,更快,更简单.既然是奔着这个目标去的,到底多快我需 ...
- python学习笔记(6)--条件分支语句
if xxxx: coding if xxxx: coding else: coding if xxxx: coding elif xxx: coding …… else: coding 或者一种简洁 ...
- git ignore 忽略 idea文件
下载了项目组的代码之后发现,一个问题,一编译就生成了很多的 .idea文件夹 还有 target文件夹,这些是不需要提交到git上的, 需要提交的时候屏蔽一下,所以需要建立一个ignore文件列表把他 ...
- 如何使用命令从linux服务器下载文件到windows
1.直接使用命令从linux下载文件到windows //登录linux服务器导出mysql数据 mysqldump -hrm-2ze8mpi5i65429l1q.mysql.rds.aliyuncs ...
- Yii2上传图片插件使用
例子: 1.在表单中: <?php $form = \yii\widgets\ActiveForm::begin([ 'options'=>[ 'class' => 'form-ho ...