A strange lift
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7565 Accepted Submission(s): 2842
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
Sample Output
3
这道题用BFS和最短路都可以~
BFS:
#include <iostream>
#include <algorithm>
#include <stdlib.h>
using namespace std;
const int M = 1005;
int n;
bool visit[M];
int buttom[M],step[M];
int loc[M];
int judge( int x )
{
if( x >= 1 && x<=n )
return 1;
return 0;
}
int BFS( int start , int end )
{
int front , tail;
front = tail = 0;
loc[front++] = start;
while( front > tail ){
int x = loc[tail++];
if( x == end )
break;
int up,down;
up = x+buttom[x];
down = x-buttom[x];
if( !visit[up] && judge(up) ){
visit[up] = true;
step[up] = step[x]+1;
loc[front++] = up;
}
if( !visit[down] && judge(down) ){
visit[down] = true;
step[down] = step[x]+1;
loc[front++] = down;
}
}
return step[end];
}
int main( )
{
int start, end;
while( scanf("%d",&n) == 1 && n ){
scanf("%d%d",&start,&end);
for( int i=1 ; i<=n ; i++ ){
scanf("%d",&buttom[i]);
visit[i] = false;
step[i] = 0;
}
if( start == end )
printf("0\n");
else{
int ans;
ans = BFS( start , end );
if( ans == 0 )
printf("-1\n");
else
printf("%d\n",step[end]);
}
}
return 0;
}
最短路:
#include <iostream>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define inf 0x7f7f7f
const int M = 250;
int dis[M],mp[M][M],buttom[M];
int n;
bool visit[M];
void Dijkstra( int start , int end )
{
for( int i=1 ; i<=n ; i++ ){
visit[i] = false;
dis[i] = mp[start][i];
}
visit[start] = true;
dis[start] = 0;
int tmp,k;
for( int i=1 ; i<=n ; i++ ){
tmp = inf;
for( int j=1 ; j<=n ; j++ ){
if( !visit[j] && tmp > dis[j] )
tmp = dis[ k = j ];
}
if( tmp == inf )
break;
visit[k] = true;
for( int j=1 ; j<=n ; j++ )
if( !visit[j] && dis[j] > mp[k][j] + dis[k] )
dis[j] = mp[k][j] + dis[k];
}
}
int main()
{
int start , end;
while( scanf("%d",&n) && n ){
scanf("%d%d",&start,&end);
for( int i=1 ; i<=n ; i++ )
for( int j=1 ; j<=n ; j++ )
mp[i][j] = inf;
for( int i=1 ; i<=n ; i++ ){
scanf("%d",&buttom[i]);
if( i+buttom[i] <= n )
mp[i][ i+buttom[i] ] = 1;
if( i-buttom[i] >= 1 )
mp[i][ i-buttom[i] ] = 1;
}
Dijkstra( start , end );
if( dis[end] == inf )
printf("-1\n");
else
printf("%d\n",dis[end]);
}
return 0;
}