A strange lift

A strange lift

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


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
 

Recommend
8600
 
#include<stdio.h>
#include<queue>
#include<algorithm>
using namespace std;
#include<string.h>
int map[220];
int sign[220];
int dir[2]={1,-1};//上下两个方向
int l,st,en,n;
struct NODE 
{
	int now,step;
}cur,next;
queue<NODE>que;
void bfs()
{
	int k;
	while(!que.empty())
	{
		cur=que.front();
		que.pop();
		if(cur.now==en)
		{
			l=1;
			printf("%d\n",cur.step);
			return ;
		}
		for(k=0;k<2;k++)
		{
			next=cur;
			next.now=next.now+dir[k]*map[next.now];
		if(next.now<=n&&next.now>=1&&!sign[next.now])//没有越界,且没有走过!!
		{
			sign[next.now]=1;
			NODE s={next.now,next.step+1};
			que.push(s);
		}
		}
	}
}

int main()
{
	int i;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		scanf("%d %d",&st,&en);		
		l=0;
	memset(sign,false,sizeof(sign));
	memset(map,0,sizeof(map));
	for(i=1;i<=n;i++)
		scanf("%d",&map[i]);
	while(que.size())
		que.pop();
	NODE s={st,0};
	que.push(s);
	sign[st]=true;
	bfs();
	if(!l)
		puts("-1");
	}
	return 0;
}
 
/*思路:BFS遍历,由于电梯只有两个方向上,下,所以BFS遍历就只需要遍历两个方向,
详细介绍如下
*/
#include<iostream>
#include<queue>
using namespace std ;
#define MAX 205
typedef struct infor
{
	int  s ,f ; //s表示按了几次电梯,即是所求,f是现在位于那层楼
}infor ;
int map[MAX] ,min1 , step[MAX] , move[2]={-1 , 1 }; //map保存每层的数据,min1保存最下按得次数,step保存每层的最小值,move表示移动的方向
queue<infor>q ; 
int n , start , end ;
bool inmap(int x)  //判断是否越界,即是否超出顶层或则超出底层
{
	if( x >= 1 && x <= n )
		return true ;
	else
		return false ;
}
void BFS()
{
	infor a;
	a.f = start ;
	a.s = 0 ;
	q.push (a);
	while(!q.empty ())
	{
		infor b = q.front () ;
		q.pop();
		for( int i = 0 ; i < 2 ; i ++)
		{
			infor c = b ;
			c.f = c.f + move[i]*map[c.f ] ;
			c.s ++ ;
			if(inmap(c.f) && step[c.f] > c.s )
			{
				if(c.f == end )
					min1 = min1 > c.s ?c.s : min1 ;
				step[c.f] = c.s  ;
				q.push(c);
			}
		}
	}

}
int main(void )
{

	while(cin>>n&&n)
	{
		min1 = 1000000 ;
		cin>>start>>end ;
		for(int  i = 1 ; i <= n ; i ++){
			cin >> map[i] ;
			step[i] = 10000000 ;
		}
		step[start] = -1 ; 
		if(start== end ){
			cout << "0" <<endl ; 
			continue ;
		}
		BFS();
		if(min1 == 1000000)
			cout << "-1"<< endl ;
		else
			cout << min1 << endl ;
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值