A strange lift HDU 1548

   A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6452    Accepted Submission(s): 2390


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 is on floor A,and you want to go to floor B,how many times at least he havt 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
 
此题用最短路做一定要注意!情况一定要分析考虑全面,一下给出一些测试数据.
1 1 1
1
0
2 2 2
3
0
2 3 3
1 2
0
1 2 3
1
-1
 
AC代码:(dijkstra)
 
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define MAX 0x3f3f3f3f
 5 using namespace std;
 6 int f[205][205],dist[205];
 7 bool flag[205];
 8 int n,a,b;
 9 int dijkstra()
10 {
11     for(int i=1;i<=n;i++){
12         dist[i]=f[a][i];
13         flag[i]=false;
14     }
15     flag[a]=true;
16     for(int i=1;i<n;i++){
17         int min=MAX,k;
18         for(int j=1;j<=n;j++){
19             if(!flag[j]&&dist[j]<min){
20                 min=dist[j];
21                 k=j;
22             }
23         }
24         if(min==MAX)break;
25         flag[k]=true;
26         if(flag[b])break;
27         for(int j=1;j<=n;j++)
28             if(!flag[j]&&dist[j]>dist[k]+f[k][j])
29                 dist[j]=dist[k]+f[k][j];
30     }
31     if(!flag[b]||dist[b]==MAX)return -1;
32     return dist[b];
33 }
34 int main()
35 {
36     while(cin>>n,n){
37         scanf("%d%d",&a,&b);
38         int c;
39         memset(f,MAX,sizeof(f));
40         for(int i=1;i<=n;i++){
41             scanf("%d",&c);
42             if(c==0)continue;
43             if(i-c>0)
44                 f[i][i-c]=1;
45             if(i+c<=n)
46                 f[i][i+c]=1;
47         }
48         if(a==b){
49             printf("0\n");
50             continue;
51         }
52         if(a<1||b<1||a>n||b>n){
53             printf("-1\n");
54             continue;
55         }
56         printf("%d\n",dijkstra());
57     }
58     return 0;
59 }

 

 
 

posted on 2012-08-14 20:15  Acmer_Roney  阅读(1368)  评论(0编辑  收藏  举报

导航