A strange lift
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 7
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"?
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.
#include<iostream>
#include<cstring>
#include<cstdio>
#define INF 0x7ffffff
#define MAX 205
using namespace std;
int map[MAX][MAX];
int visit[MAX],dis[MAX];
int N,A,B;
void Dijkstra(int s,int n)
{
int i,j;
for(i=1;i<=n;i++) dis[i]=map[s][i],visit[i]=false;
dis[s]=0;//important
visit[s]=true;// important
for(i=1;i<=n;i++)
{
int k,tmp=INF;
for(j=1;j<=n;j++)
if(!visit[j]&&dis[j]<tmp) tmp=dis[k=j];
if(tmp==INF) break; //important
visit[k]=true;
for(j=1;j<=n;j++)
if(!visit[j]&&dis[j]>dis[k]+map[k][j])
dis[j]=dis[k]+map[k][j];
}
}
int main()
{
int i,j,k;
while(scanf("%d",&N),N)
{
scanf("%d%d",&A,&B);
// memset(map,INF,sizeof(map));
for(i=1;i<=N;i++)
for(j=1;j<=N;j++)
map[i][j]=INF;
for(i=1;i<=N;i++)
{
scanf("%d",&k);
if(i-k>=1) map[i][i-k]=1;
if(i+k<=N) map[i][i+k]=1;
}
Dijkstra(A,N);
if(dis[B]==INF) printf("-1\n");
else printf("%d\n",dis[B]);
}
return 0;
}