178人阅读 评论(0)

A strange lift

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

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
5 1 5
3 3 1 2 5
0

Sample Output
3

bfs code：
#include<stdio.h>
#include<iostream>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int flor[210],n,s,t,vis[210];
struct node {
int ord,ste;
};
int judge(node tem)
{
if(tem.ord<=0 || tem.ord>n)
return 0;
return 1;
}
int bfs()
{
queue<node> q;
node tem,next;
node a;
a.ord=s;
vis[s]=1;
a.ste=0;
q.push(a);
while(!q.empty())
{
tem=q.front();
q.pop();
if(tem.ord==t)
return tem.ste;
next.ord=tem.ord+flor[tem.ord];
if(judge(next) && !vis[next.ord])
{
next.ste=tem.ste+1;
q.push(next);
vis[next.ord]=1;
}
next.ord=tem.ord-flor[tem.ord];
if(judge(next) && !vis[next.ord])
{
next.ste=tem.ste+1;
q.push(next);
vis[next.ord]=1;
}
}
return -1;
}
int main()
{
//freopen("input.txt","r",stdin);
int i,j;
while(scanf("%d",&n),n)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&s,&t);
for(i=1;i<=n;i++)
scanf("%d",&flor[i]);
printf("%d\n",bfs());
}
return 0;
}
dij code：
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include<iostream>
using namespace std;
const int inf = 1<<30;
int map[210][210],dis[210],vis[210],n,s,t;
void dij()
{
int i,j;
for(i=1; i<=n; i++)
dis[i]=map[s][i];
dis[s]=0;
vis[s]=1;
int t;
for(i=1;i<n;i++)
{
int minn=inf;
for(j=1;j<=n;j++)
{
if(dis[j]<minn && vis[j]==0)
{
t=j;
minn=dis[j];
}
}

if(minn==inf)
break;
vis[t]=1;

for(j=1;j<=n;j++)
{
if(dis[j]>dis[t]+map[t][j] && !vis[j])
dis[j]=dis[t]+map[t][j];
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
int i,j,ttt,tt;
while(scanf("%d",&n),n)
{
scanf("%d%d",&s,&t);
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
map[i][j]=inf;
if(i==j)
map[i][j]=0;
}
for(i=1; i<=n; i++)
{
scanf("%d",&ttt);
if(i+ttt<=n)
map[i][i+ttt]=1;
if(i-ttt>0)
map[i][i-ttt]=1;
}
dij();
printf("%d\n",dis[t]==inf?-1:dis[t]);
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：49577次
• 积分：2286
• 等级：
• 排名：第16955名
• 原创：187篇
• 转载：3篇
• 译文：0篇
• 评论：1条
评论排行
最新评论