HDU 1548 A strange lift (广搜)
Problem Description
There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two buttons: up and down.When you at floor i,if you press the button "UP" , you will go up Ki floor,i.e,you will go to the i+Ki th floor,as the same, if you press the button "DOWN" , you will go down Ki floor,i.e,you will go to the i-Ki th floor. Of course, the lift can't go up high than N,and can't go down lower than 1. For example, there is a buliding with 5 floors, and k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5.Begining from the 1 st floor,you can press the button "UP", and you'll go up to the 4 th floor,and if you press the button "DOWN", the lift can't do it, because it can't go down to the -2 th floor,as you know ,the -2 th floor isn't exist.
Here comes the problem: when you are on floor A,and you want to go to floor B,how many times at least he has to press the button "UP" or "DOWN"?
Input
The input consists of several test cases.,Each test case contains two lines.
The first line contains three integers N ,A,B( 1 <= N,A,B <= 200) which describe above,The second line consist N integers k1,k2,....kn.
A single 0 indicate the end of the input.
Output
For each case of the input output a interger, the least times you have to press the button when you on floor A,and you want to go to floor B.If you can't reach floor B,printf "-1".
Sample Input
5 1 5
3 3 1 2 5
0
Sample Output
3
分析:
开始一些BFS练习了,先来一道简单的,这是一栋大楼,有一部很奇怪的电梯,电梯只有两个按钮,
UP和DOWN,每个楼层有一个数字num,如果你在第2层,你按UP,电梯会到达 num+2层,按DOWN亦然,
到达2-num层,求一个人从一个楼层要去另一个楼层,需要按几步?
(这个电梯够让人捉急的!)
当然如果碰到0层或者-1层都不能到达,碰到大于刚开始的n层的也不能到达,
如果永远到不了需要去的那一层,就输出-1了。
广搜,队列做,标准的BFS题目呀,很容易就AC了。。
代码:
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int A,B;
int a[500];
int bj[500];
struct zuobiao
{
int x,output;
} now,nex;
void bfs(int x)
{
queue<zuobiao >s;
memset(bj,0,sizeof(bj));
bj[x]=1;
now.x=x;
now.output=0;
s.push(now);
while(!s.empty())
{
now=s.front();
if(now.x==B)
{
printf("%d\n",now.output);
return ;
}
s.pop();
for(int i=0; i<2; i++)
{
if(i==0)nex.x=now.x+a[now.x];
if(i==1)nex.x=now.x-a[now.x];
if(nex.x>=1&&nex.x<=200&&bj[nex.x]==0)
{
bj[nex.x]=1;
nex.output=now.output+1;
s.push(nex);
}
}
}
printf("-1\n");
return ;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)break;
scanf("%d%d",&A,&B);
memset(a,0,sizeof(a));
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
}
bfs(A);
}
}
HDU 1548 A strange lift (广搜)的更多相关文章
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- hdu 1548 A strange lift
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...
- HDU 1548 A strange lift (bfs / 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1548 A strange lift (Dijkstra)
A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...
- HDU 1548 A strange lift(BFS)
Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift (最短路/Dijkstra)
题目链接: 传送门 A strange lift Time Limit: 1000MS Memory Limit: 32768 K Description There is a strange ...
- HDU 1548 A strange lift 搜索
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 1548 A strange lift(最短路&&bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
随机推荐
- 活学活用wxPython
http://www.czug.org/python/wxpythoninaction/
- SQL SERVER技术内幕之6 集合查询
1.定义 集合运算会对两个输入查询的结果集进行逐行比较,根据比较结果和所使用的集合运算来确定某一行是否应该包含在集合运算的结果中.因为集合运算是针对集合之间进行的计算,所以集合运算涉及的两个查询不能包 ...
- Bootstrap 轮播图的使用和理解
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...
- Qt编码设置
1.Qt Creator -> 工具 -> 选项 -> 环境 - >概要 -> 语言 Qt Creator本身界面的语言选择,与cpp文件编码无关,与可执行文件显示 ...
- C# 面向对象——继承
继承:代码文字结合理解 class Program { static void Main(string[] args) { //Student s = new Student(); //Driver ...
- POJ1236:Network of Schools——题解
http://poj.org/problem?id=1236 首先还是缩点,然后入度为0的点的个数就是你要投文件个数. 然后我们对于入度和出度为0的点的个数取最大值即为答案. (简单证明:入度和出度为 ...
- BZOJ1085:[SCOI2005]骑士精神——题解+IDA*粗略讲解
http://www.lydsy.com/JudgeOnline/problem.php?id=1085 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空 ...
- HDOJ(HDU).1058 Humble Numbers (DP)
HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...
- JavaScript数据类型转换方法汇总
转换为布尔型 用两次非运算(!): 1 !!5 ==> true 用布尔型的构造函数: 1 new Boolean(5) == > true 值转换为布尔类型为false:0,+0,-0, ...
- Consul 入门
1. 什么是Consul? Consul 有很多组件,对于整体来说,它是一个服务发现和服务配置的工具,它提供了一下特性: 服务发现 健康检查 KV存储 多数据中心 2.安装Consul 以下是在 Ce ...