HDU 2717
Catch That Cow
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8764 Accepted Submission(s): 2762
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
//此题是在一条水平线上追赶羊。分为三种情况 x-1 /x+1 /2*x 注意边界就可以
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n,start,stop;
int p[210];
int vis[210];
struct Node
{
int floor;
int step;
}; int bfs(int flo)
{
queue <Node> q;
memset(vis,0,sizeof(vis));
Node a;
a.floor=flo,a.step=0;
q.push(a);
while(!q.empty())
{
Node b=q.front();
q.pop();
vis[b.floor]=1;
if(b.floor==stop) return b.step;
for(int i=0;i<2;i++) //0 up 1 down
{
Node c=b;
if(i==0)
c.floor=c.floor+p[c.floor];
else
c.floor=c.floor-p[c.floor]; if(!vis[c.floor]&&c.floor>=1&&c.floor<=n)
{
c.step++;
q.push(c);
vis[c.floor]=1;
}
}
}
return -1;
} int main()
{
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&start,&stop);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("%d\n",bfs(start));
}
return 0;
}
HDU 2717的更多相关文章
- 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 ...
- 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(常规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,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- HDU 2717 Catch That Cow
简单的广搜: #include <cstdio> #include <queue> using namespace std; ],step[]; int n,start,end ...
- HDU 2717(* bfs)
题意是在一个数轴上,每次可以一步到达当前位置数值的 2 倍的位置或者数值 +1 或数值 -1 的位置,给定 n 和 k,问从数值为 n 的位置最少多少步可以到达数值为 k 的位置. 用广搜的方法,把已 ...
- hdu 2717 从n点走到k点 (BFS)
在横坐标上 从n点走到k点 至少要几步 可以到 n+1 n-1 n*2这3个点 Sample Input5 17 Sample Output4 #include <iostream> #i ...
- hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- Spark Streaming基础概念
为了更好地理解Spark Streaming 子框架的处理机制,必须得要自己弄清楚这些最基本概念. 1.离散流(Discretized Stream,DStream):这是Spark Streamin ...
- view在使用shape属性加圆角的同时,用代码修改其他background属性(例如颜色)不生效
项目中一个TextView控件设置了shape属性,给其加了圆角,如下: houlder.mtxtGovernmentType.setBackgroundResource(R.drawable.tv_ ...
- 【SQL】日期型函数
1. SYSTATE 用来返回系统当前时间 SQL> select sysdate from dual; SYSDATE ------------------- 2017-03-03 09:49 ...
- asp.net 正则表达式 得到图片url 得到汉字
//取图片 MatchCollection matchs = Regex.Matches(AskText,@"<img\s[^> ]*src=( ...
- QtUI设计:设置控件透明
QT设置按钮控件透明: 代码: //设置按钮 背景 前景 this->ui->ShowCvRGB->setStyleSheet(QString("color:rgba(25 ...
- jQuery+pjax简单示例汇总
pjax 是一个jQuery插件,它使用 ajax 和 pushState 来实现快速的浏览体验,包括真正的固定链接,页面标题和工作返回按钮. ajax缺点是破坏了浏览器的前进后退,因为ajax的请求 ...
- angular搭建
脚手架工具:angular-cli 1. npm install -g @angular/cli 2.ng new xxx 3.cd xxx , ng serve
- mount 命令总结
配置CnetOS 7.4 本地yum源,记录下遇到的ISO镜像挂载问题,使用 blkid 命令可以查看设备的UUID.Label.文件系统类型(iso镜像文件系统类型iso9660) [root@lo ...
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...
- 【转载】使用IntelliJ IDEA提示找不到struts-default文件
创建strus,参考文如下: https://blog.csdn.net/u010358168/article/details/79769137 使用IntelliJ IDEA创建struts2工程时 ...