A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6572 Accepted Submission(s): 2443
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 /* 功能Function Description: HDOJ-1548 2 开发环境Environment: DEV C++ 4.9.9.1 3 技术特点Technique: 4 版本Version: 5 作者Author: 可笑痴狂 6 日期Date: 20120821 7 备注Notes: 8 这题一看就是BFS题,但是老师让用最短路径做,谁知道测试数据太变态了用最短路径WA了半天 9 用BFS时注意要访问标记一下,要不会MLE 10 */ 11 /* 12 //代码一:(用队列实现广搜) 13 #include<cstdio> 14 #include<queue> 15 using namespace std; 16 17 int button[205]; 18 struct node 19 { 20 int floor; 21 int time; 22 }; 23 24 int BFS(int a,int b,int n) 25 { 26 27 queue<node> q; 28 bool visit[205]; //没有访问标记会MLE 29 node tmp,s; 30 memset(visit,false,sizeof(visit)); 31 visit[a]=true; 32 s.floor=a; 33 s.time=0; 34 q.push(s); 35 while(!q.empty()) 36 { 37 s=q.front(); 38 q.pop(); 39 if(s.floor==b) 40 return s.time; 41 else 42 { 43 for(int i=1;i<=2;++i) 44 { 45 if(i==1) 46 tmp.floor=s.floor+button[s.floor]; //向上 47 else 48 tmp.floor=s.floor-button[s.floor]; //向下 49 if(tmp.floor<=0||tmp.floor>n||visit[tmp.floor]) 50 continue; 51 else 52 { 53 tmp.time=s.time+1; 54 visit[tmp.floor]=true; 55 q.push(tmp); 56 } 57 } 58 } 59 } 60 return -1; 61 } 62 63 int main() 64 { 65 int n,a,b,i; 66 while(scanf("%d",&n),n) 67 { 68 scanf("%d%d",&a,&b); 69 for(i=1;i<=n;++i) 70 scanf("%d",&button[i]); 71 printf("%d\n",BFS(a,b,n)); 72 } 73 return 0; 74 } 75 */ 76 77 //代码二:----dijkstra 78 #include<stdio.h> 79 #include<string.h> 80 #define inf 0x3fffffff 81 int map[205][205]; 82 83 int dijks(int s,int t,int n) 84 { 85 bool visit[205]; 86 int dis[205]; 87 int i,j,k,min; 88 memset(visit,false,sizeof(visit)); 89 visit[s]=true; 90 for(i=1;i<=n;++i) 91 dis[i]=map[s][i]; 92 for(i=1;i<n;++i) 93 { 94 min=inf; 95 k=1; 96 for(j=1;j<=n;++j) 97 { 98 if(!visit[j]&&min>dis[j]) 99 { 100 min=dis[j]; 101 k=j; 102 } 103 } 104 if(k==t||min==inf) 105 break; 106 visit[k]=true; 107 for(j=1;j<=n;++j) 108 { 109 if(!visit[j]&&dis[k]+map[k][j]<dis[j]) 110 dis[j]=dis[k]+map[k][j]; 111 } 112 } 113 if(dis[t]==inf) 114 return -1; 115 else 116 return dis[t]; 117 } 118 119 int main() 120 { 121 int n,a,b,i,j; 122 int button[205]; 123 while(scanf("%d",&n),n) 124 { 125 scanf("%d%d",&a,&b); 126 for(i=1;i<=n;++i) 127 scanf("%d",&button[i]); 128 if(a==b) //注意这点,测试数据有点变态,超出楼层高度的数据 129 { 130 printf("0\n"); 131 continue; 132 } 133 if(a>n||b>n) 134 { 135 printf("-1\n"); 136 continue; 137 } 138 for(i=1;i<=n;++i) 139 for(j=1;j<i;++j) 140 map[i][j]=map[j][i]=inf; 141 for(i=1;i<=n;++i) 142 { 143 if(i+button[i]<=n) 144 map[i][i+button[i]]=1; 145 if(i-button[i]>0) 146 map[i][i-button[i]]=1; 147 } 148 printf("%d\n",dijks(a,b,n)); 149 } 150 return 0; 151 }
功不成,身已退