HDU 1548 <BFS+标记+找最短>

来源:互联网 发布:淘宝的一元秒杀在哪里 编辑:程序博客网 时间:2024/06/14 02:32

the reason of failure:MLE,内存超出范围...一直看错题意,花了两个小时debug,可见题意的重要

thinking:由于电梯在i楼时向上是i+ki,向下是k-ki,那么每次在i发生的情况都一样,所以可以用walked标记走过

题意:有一个电梯有N层楼,从i到j最少需要按多少次电梯,每次按电梯向上或者向下,高度为ki,也就是说按了向上后高度变为i+ki,向下变为j+kj.

A strange lift
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1548

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 53 3 1 2 50

Sample Output

3
代码:

#include <iostream>#include <queue>#include <string.h>using namespace std;struct ttt{int high,id;};int l[205];bool mark[205];int main(){freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);int n,end,i,f4;ttt f2;ttt now1;queue<ttt>qq; bool f1;while(cin >> n&&n!=0){cin >> now1.high >> end;for(i=1;i<=n;i++)cin >> l[i];now1.id=0;f1=0;f4=-1;memset(mark,0,sizeof(mark));while(!qq.empty())qq.pop();qq.push(now1);mark[now1.high]=1;while(!qq.empty()&&now1.id<n+1){now1=qq.front();qq.pop();if(now1.high==end){f1=1;break;}else{now1.id++;f2=now1;f2.high=now1.high+l[now1.high];if(f2.high<=n&&!mark[f2.high]){qq.push(f2);mark[f2.high]=1;}f2.high=now1.high-l[now1.high];if(f2.high>0&&!mark[f2.high]){mark[f2.high]=1;qq.push(f2);}}}if(f1==1)cout << now1.id << endl;else cout << "-1" << endl;}}



0 0
原创粉丝点击