本文主要是介绍A strange lift,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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
题意:
给出楼层数和起点终点,接下来给出每一层可以改变的层数,当然要在1-n范围内
很显然是一个最短路问题,将楼层当做结点,各个楼层可以到达则把距离设为1
代码:
//一个人的旅行
/*#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1005;//段点数
bool book[maxn];
int dis[maxn],first[maxn],cnt;
struct node
{
int u;//next
int v,w;
node(int uu=0,int vv=0,int ww=0):u(uu),v(vv),w(ww){}
}e[maxn*maxn];
inline void intt()
{
cnt=0;
memset(first,-1,sizeof(first));
memset(book,0,sizeof(book));
//memset(dis,0x3f,sizeof(book));
}
inline void add(int u,int v,int w)
{
e[++cnt]=node(first[u],v,w);
first[u]=cnt;
}
void spfa()
{
for(int i=0;i<1005;i++)
dis[i]=inf;
queue<int>q;
q.push(0);
dis[0]=0;
book[0]=1;
while(!q.empty())
{
//printf("12\n");
int u=q.front();
q.pop();
book[u]=0;
for(int i=first[u];~i;i=e[i].u)
{
int v=e[i].v;
int w=e[i].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(book[v]==0)
{
q.push(v);
book[v]=1;
}
}
}
}
}
int main()
{
int t,s,d;
int u,v,w;
while(scanf("%d %d %d",&t,&s,&d)!=EOF)
{
intt();
for(int i=0;i<t;i++)
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
int a;
for(int i=0;i<s;i++)
{
scanf("%d",&a);
add(a,0,0);
add(0,a,0);
}
spfa();
//printf("123\n");
int minn=0x3f3f3f3f;
for(int i=0;i<d;i++)
{
scanf("%d",&a);
minn=min(minn,dis[a]);
}
printf("%d\n",minn);
}
return 0;
}*/
//B
/*#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int dis[205];
int e[205][205];
bool book[205];
int n,m;
void intt()
{
memset(book,0,sizeof(book));
for(int i=0;i<n;i++)
dis[i]=inf;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
e[i][j]=inf;
}
}
int main()
{
int t1,t2,t3;
while(scanf("%d %d",&n,&m)!=EOF)
{
intt();
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&t1,&t2,&t3);
e[t1][t2]=min(e[t1][t2],t3);
e[t2][t1]=e[t1][t2];
}
int st,en;
scanf("%d %d",&st,&en);
for(int i=0;i<n;i++)
dis[i]=e[st][i];
book[st]=1;
dis[st]=0;
for(int i=0;i<n;i++)
{
int minn=inf;
int u;
for(int j=0;j<n;j++)
{
if(book[j]==0&&dis[j]<minn)
{
u=j;
minn=dis[j];
}
}
book[u]=1;
for(int v=0;v<n;v++)
{
if(e[u][v]<inf)
{
if(dis[v]>dis[u]+e[u][v])
dis[v]=dis[u]+e[u][v];
}
}
}
if(dis[en]!=inf)
printf("%d\n",dis[en]);
else
printf("-1\n");
}
return 0;
}
*/
//c
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int dis[205];
int e[205][205];
bool book[205];
int n,m;
void intt()
{
memset(book,0,sizeof(book));
for(int i=0;i<n;i++)
dis[i]=inf;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
e[i][j]=inf;
}
}
int main()
{
int t1,t2,t3;
int st,en;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
scanf("%d %d",&st,&en);
st--;
en--;
intt();
int a;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
if(i+a<n)
e[i][i+a]=1;
if(i-a>=0)
e[i][i-a]=1;
}
for(int i=0;i<n;i++)
dis[i]=e[st][i];
book[st]=1;
dis[st]=0;
for(int i=0;i<n;i++)
{
int minn=inf;
int u;
for(int j=0;j<n;j++)
{
if(book[j]==0&&dis[j]<minn)
{
u=j;
minn=dis[j];
}
}
book[u]=1;
for(int v=0;v<n;v++)
{
if(e[u][v]<inf)
{
if(dis[v]>dis[u]+e[u][v])
dis[v]=dis[u]+e[u][v];
}
}
}
if(dis[en]!=inf)
printf("%d\n",dis[en]);
else
printf("-1\n");
}
return 0;
}
这篇关于A strange lift的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!