题目地址:HDU 1548 http://acm.hdu.edu.cn/showproblem.php?pid=1548
A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19009 Accepted Submission(s): 7025
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"?
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.
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
题目大意:
每层楼的电梯,只有两个按钮:上、下。你只能上或下所给出的层数,当然,电梯不能升得很高,而且不能低于1。例如,有一个5层的建筑,k1 = 3, k2 = 3,k3 = 1,k4 = 2, k5 = 5。从1楼开始,你可以按下按钮“上”,你会走到4楼,如果你按“下”的按钮,电梯不能做它,因为它不能去 -2 楼,你知道的,在 -2 楼是不存在的。 问题是:当你在地上一个,你要去地乙,至少按多少次按钮“上”或“下”?
解题思路
这求最短路问题,使用简单的bfs就可以解决,注意起点终点位置一致问题、不能越界问题、
访问数组初始化问题、队列清空问题。
代码:
#include<cstdio>
#include<queue>
#include<string.h>
using namespace std;
struct node
{
int x,t;//x表示的是当前层数,t表示总步数;cz表示上下走
}s;//s表示起点
queue<node> q;
int n,a,b;
int p[210]={};
bool vis[210]={};//存是否走过当前层
int bfs()
{
q.push(s);
vis[s.x]=1;
while (!q.empty())
{
node now,nnew;
now=q.front();
q.pop();
//向上
nnew.x=now.x+p[now.x];
nnew.t=now.t+1;
if(!vis[nnew.x]&&nnew.x<210)
{
q.push(nnew);
vis[nnew.x]=1;
}
if(nnew.x==b) return nnew.t;
//向下
nnew.x=now.x-p[now.x];
nnew.t=now.t+1;
if(nnew.x==b) return nnew.t;
if(nnew.x>0&&!vis[nnew.x])
{
q.push(nnew);
vis[nnew.x]=1;
}
}
return -1;
}
int main()
{
while(scanf("%d",&n)&&n!=0)
{
scanf("%d%d",&a,&b);
s.x=a;
s.t=0;
for(int i=1;i<=n;++i)
scanf("%d",&p[i]);
if(a==b) printf("0\n");
else printf("%d\n",bfs());
memset(vis,0,sizeof(vis));//初始化访问数组!!
while(!q.empty())//队列清空!!
{
q.pop();
}
}
return 0 ;
}
探讨了一种特殊电梯的工作原理及其楼层移动限制,并通过BFS算法解决从任意楼层A到达另一楼层B所需的最少按键次数。
2260

被折叠的 条评论
为什么被折叠?



