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

C++版本一(DFS)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,a,b, ans;
int d[210];
int vis[210];

void dfs(int a,int step)
{
    if (a==b){
        if(ans>step)
        ans=step;
        return;
    }
    if(ans>step) {
        if(a+d[a-1]<=n&&vis[a+d[a-1]]==0){
            vis[a+d[a-1]]=1;
            dfs(a+d[a-1],step+1);
            vis[a+d[a-1]]=0;
        }

        if(a-d[a-1]>=1&&vis[a-d[a-1]]==0){
            vis[a-d[a-1]]=1;
            dfs(a-d[a-1],step+1);
            vis[a-d[a-1]]=0;
        }

    }

}
int main()
{
    while (scanf("%d",&n)!=EOF){
        if (n==0)   break;
        scanf("%d%d",&a,&b);
        memset(d,0,sizeof(d));
        memset(vis,0,sizeof(vis));

        for(int i=0;i<n;i++){
            scanf("%d",&d[i]);
        }
        vis[a]=1;
        ans=n+1;
        dfs(a,0);
        if(ans==n+1)printf("-1\n");
        else printf("%d\n",ans);



    }

    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二(DFS)

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm> 
#include<queue>
#include<string>
#include<set>
using namespace std;
int n,a,b;
int c[201],vis[201];
void dfs(int x,int s) 
{
	if(s==vis[b]) return;
	if(x==b) 
	{
		vis[b]=min(vis[b],s);//更新最小步数 
		return;
	}
	if(x+c[x]<=n&&vis[x+c[x]]>s+1)//往上能走且到达那层的步数比当前数值小 
	{
		vis[x+c[x]]=s+1;//更新走到当前楼层需要的步数 
		dfs(x+c[x],s+1);
	}
	if(x-c[x]>0&&vis[x-c[x]]>s+1)//往下走 
	{
		vis[x-c[x]]=s+1;
		dfs(x-c[x],s+1);
	}
}
int main()
{
	while (scanf("%d",&n)!=EOF&&n) 
	{
		scanf("%d%d",&a,&b);
		for(int i=1;i<=n;i++) 
		{
			scanf("%d",&c[i]);
			vis[i]=201;
		}
		vis[a]=0;//必须为0 
		dfs(a,0);
		if (vis[b]!=201) printf("%d\n",vis[b]);
		else printf("-1\n");
	}
	return 0;
}

C++版本三(BFS)

#include<stdio.h>
#include<queue>
using namespace std;
int n,a,b;
int c[201];
int vis[201];
void bfs()
{
	queue<int>q;
	q.push(a);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		if(x==b) return;//能走到 
		if(x+c[x]<=n&&vis[x+c[x]]>vis[x]+1) //向上能走且当前步数小于记录的值 
		{
			vis[x+c[x]]=vis[x]+1;
			q.push(x+c[x]);
		}
		if(x-c[x]>0&&vis[x-c[x]]>vis[x]+1) //同理 
		{
			vis[x-c[x]]=vis[x]+1;
			q.push(x-c[x]);
		}
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF&&n!=0) 
	{
		scanf("%d%d",&a,&b);
		for(int i=1;i<=n;i++) 
		{
			scanf("%d",&c[i]);
			vis[i]=201;
		}
		vis[a]=0;//必须为0 
		bfs();
		if(vis[b]==201) printf("-1\n");
		else printf("%d\n",vis[b]);
	}
	return 0;
}

C++版本四(Dijkstra)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
 
const int inf = 1<<30;
 
int n;
int map[205][205];
int a[205],cnt;
int vis[205],cast[205];
 
void Dijkstra(int s,int e)
{
    int i,j,min,pos;
    memset(vis,0,sizeof(vis));
    for(i = 0; i<n; i++)
        cast[i] = map[s][i];
    cast[s] = 0;
    vis[s] = 1;
    for(i = 1; i<n; i++)
    {
        min = inf;
        for(j = 0; j<n; j++)
        {
            if(cast[j]<min && !vis[j])
            {
                pos = j;
                min = cast[j];
            }
        }
        if(min == inf)
            break;
        vis[pos] = 1;
        for(j = 0; j<n; j++)
        {
            if(cast[pos]+map[pos][j]<cast[j] && !vis[j])
                cast[j] = cast[pos]+map[pos][j];
        }
    }
}
 
int main()
{
    int i,j,s,e,x,y;
    while(~scanf("%d",&n),n)
    {
        scanf("%d%d",&s,&e);
        s--,e--;
        for(i = 0; i<n; i++)
            for(j = 0; j<n; j++)
                map[i][j] = inf;
        for(i = 0; i<n; i++)
        {
            scanf("%d",&a[i]);
            if(i+a[i]<n)
                map[i][i+a[i]] = 1;
            if(i-a[i]>=0)
                map[i][i-a[i]] = 1;
        }
        Dijkstra(s,e);
        printf("%d\n",cast[e]==inf?-1:cast[e]);
    }
 
    return 0;
}

C++版本五

/*
*@Author:   STZG
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
//#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
typedef __int128 lll;
const int N=1010;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,s,e;
int map[N][N];
bool vis[N];
int dist[N];
void init(){
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            map[i][j]=(i==j?0:INF);
    memset(vis,false,sizeof(vis));
    //memset(dist,0,sizeof(dist));
}
void Dijkstra(){
    for(int i=1;i<=n;i++)
        dist[i]=map[s][i];
    dist[s]=0;
    vis[s]=1;
    for(int i=1;i<n;i++){
        int minl=INF,k=0;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&dist[j]<minl){
                minl=dist[j];
                k=j;
            }
        }
        if(k==0)
            break;
        vis[k]=1;
        for(RI j=1;j<=n;j++){
            if(!vis[j]&&dist[j]>map[k][j]+minl){
                dist[j]=map[k][j]+minl;
            }
        }
    }
}

int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(~scanf("%d",&n)&&n){
        scanf("%d%d",&s,&e);
        init();
        int a;
        for(int i=1;i<=n;i++){
            scanf("%d",&a);
            if(i+a<=n)
                map[i][i+a]=1;
            if(1<=i-a)
                map[i][i-a]=1;
        }
        Dijkstra();
        cout<<(dist[e]==INF?-1:dist[e])<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司7个岗位
点赞 评论 收藏
分享
05-29 22:11
门头沟学院 Java
Elastic90:抛开学历造假不谈,这公司的招聘需求也挺怪的,Java开发还要求你有图文识别、移动端开发和c++的经验,有点逆天了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务