hdu1548 A strange lift (简单bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548

A strange lift

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 15974    Accepted Submission(s): 5973

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

【题意】

一个奇怪的电梯,每一层有一个数Ki,在每一层有两个选择,选择上升Ki层,或者下降Ki层,现在给你起始层数 和目标层数,问最少几次操作电梯能到达目标层数

如果不能则输出-1;

【tips】

笔者wa的原因是没考虑周全,忘了测试当起始层数 和目标层数相同时的情况。

【bfs代码】

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int k[250];		int t;
int A,B;
bool vis[250];
struct node{
	int x,times;
}no;
int bfs(int a){
	queue<node>Q;
	while(!Q.empty())
		Q.pop();
	no.x=a;no.times=0;
	Q.push(no);
	vis[no.x]=1;
	while(!Q.empty()){
		node now=Q.front();
		Q.pop();
		if(now.x==B)
			return now.times; //在开始加上判断条件 就ac了
		for(int i=0;i<2;i++){
			node next;
			if(i==0){
				next.x=now.x+k[now.x-1];  //上升
			}
			else if(i==1){
				next.x=now.x-k[now.x-1]; //下降
			}
			if(next.x>=1&&next.x<=t&&!vis[next.x]){
				next.times=now.times+1;
				vis[next.x]=1; //到达过的层数直接标记就可以了, 因为如果再回到该层就循环了
				if(next.x==B)
					return next.times;
				else
				{
					Q.push(next);
				}
			}
		}
	}
	return -1;
}
int main(){
	while(cin>>t&&t){
		memset(vis,0,sizeof(vis));
		cin>>A>>B;
		for(int i=0;i<t;i++){
			cin>>k[i];
		}
		int ans=bfs(A);
		cout<<ans<<endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 20:31:51

hdu1548 A strange lift (简单bfs)的相关文章

hdu1548 A strange lift(bfs 或Dijkstra最短路径)

1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #define M 205 6 #define INF 0x3f3f3f3f 7 using namespace std; 8 9 int map[M][M]; 10 int d[M], vis[M]; 11 int n; 12 int b, e; 13 14 void Dijkstra(){

HDU1548——A strange lift(最短路径:dijskstra算法)

A strange lift DescriptionThere 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 "U

hdu1548 A strange lift(bfs)

A B C D E F G C - A strange lift Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1548 Appoint description: System Crawler (2016-05-23) Description There is

hdu1548——A strange lift

A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12230    Accepted Submission(s): 4656 Problem Description There is a strange lift.The lift can stop can at every floor as you want

E - A strange lift 【BFS】

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 K

HDU1548:A strange lift

A strange lift Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 29 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description There is a strange lif

HDU1548 A strange lift BFS 简单题

一个电梯有n层,每一层有一个数k[i],和2个按钮,UP和DOWN,表示从这一层可以到达i+k[i] 或i-k[i] . 给出a,b,问从a到b 最少需要按多少下按钮. 直接bfs. 1 #include<cstdio> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 const int maxn=210; 6 int n,a,b; 7 int k[maxn]; 8 bool vis[maxn

HDU 1548 A strange lift【BFS】

题意:给出一个电梯,给出它的层数f,给出起点s,终点g,以及在每一层能够上或者下w[i]层,问至少需要按多少次按钮到达终点. 和POJ catch that cow一样,直接用了那一题的代码,发现一直wa, 后来才发现,POJ catch that cow是单组输入的,所以每次调用的时候不用清空队列,而这一题一次输入有多组数据--- 用这个清空队列 while(!q.empty()) q.pop(); #include<iostream> #include<cstdio> #inc

HDU 1548 A strange lift(Dijkstra,简单BFS)

题目大意: 电梯有两个选项向上或向下,每层楼有一个参数ki,代表电梯可以再该楼层的基础上向上或向下移动ki层,限制条件是向上不能超过楼层总数n,向下不能少于一.输入总层数n和当前所在层数以及目标层数,然后是n个数分别代表第i层的移动范围.输出最少移动次数,若不可达,输出-1. 解题思路: 1.用Dijkstra算法,首先构建邻接矩阵,注意在构造时,要考虑i-k[i]<1和i+k[i]>n,i代表当前所在层. 1 #include<string.h> 2 #include<st