A strange lift

点击打开链接

题意:有n层楼层,现在在每一层有两个按钮,分别为up和down,按动按钮时,可以向上或向下跳动num[ i ]层;问能否以最少的次数从A到B层,不能输出-1;

解析:构图,将从i层到按动按钮后跳转的楼层,看作连通状态,赋值为1,这样就转换成单源最短路问题;

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

const int maxn = 500;
const int Max = 0x3f3f3f3f;
int num[ maxn ], mapp[ maxn ][ maxn ], dis[ maxn ], vis[ maxn ];
int n, start, end;

void Dijkstra( int start ){
	memset( vis, 0, sizeof( vis ) );
	vis[ start ] = 1;
	for( int i = 1; i <= n; ++i ){
		dis[ i ] = mapp[ start ][ i ];
	}
/*	for( int i = 0; i <= n; ++i ){
		cout << dis[ i ] << endl;
	}*/
	dis[ start ] = 0;
	for( int i = 1; i <= n; ++i ){
		int temp = Max, k;
		for( int j = 1; j <= n; ++j ){
			if( !vis[ j ] && temp > dis[ j ] ){
				temp = dis[ k = j ];
			}
		}
		if( temp == Max )
			break;
		vis[ k ] = 1;
		for( int j = 1; j <= n; ++j ){
			if( !vis[ j ] && dis[ j ] > dis[ k ] + mapp[ k ][ j ] ){
				dis[ j ] = dis[ k ] + mapp[ k ][ j ];
			//	vis[ j ] = 1;
			}
		}
	}
		/*for( int i = 0; i <= n; ++i ){
		cout << dis[ i ] << endl;
	}*/
}

int main(){
	while( scanf( "%d", &n ) != EOF ){
		if( !n )
			break;
		scanf( "%d%d", &start, &end );
		for( int i = 0; i <= n; ++i ){
			for( int j = 0;j <= n; ++j ){
				mapp[ i ][ j ] = Max;
			}
		}
		for( int i = 1; i <= n; ++i ){
			scanf( "%d", &num[ i ] );
		}
		for( int i = 1; i <= n; ++i ){
			if( i + num[ i ] <= n ){
				mapp[ i ][ i + num[ i ] ] = 1;
			}
			if( i - num[ i ] >= 1 ){
				mapp[ i ][ i - num[ i ] ] = 1;
			}
		}
		Dijkstra( start );
		if( dis[ end ] == Max )
			puts( "-1" );
		else{
			printf( "%d\n", dis[ end ] );
		}
	}
	return 0;
}

A strange lift,布布扣,bubuko.com

时间: 2024-08-08 05:38:37

A strange lift的相关文章

HDU 1548 A strange lift【不错的最短路,spfa】

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

HDU 1548.A strange lift

A strange lift Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1548 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)

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

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

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

杭电 1548 A strange lift(广搜)

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): 11341    Accepted Submission(s): 4289 Problem Description There is a strange lift.T

hdu1584 A strange lift (电梯最短路径问题)

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

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

HDU 1548 (最基础的BFS了) A strange lift

这是一维的BFS,而且没有什么变形,应该是最基础的BFS了吧 题意: 有这样一个奇葩的电梯,你在第i层的时候你只能选择上或者下Ki层,也就是你只能从第i层到达i+Ki或者i-Ki层.当然电梯最低只能在1层最高只能在n层. 给出起点和终点问最少需要多少次才能到达终点,如果不能到达输出-1 没有什么好解释的了,如此单纯的一道题,只要不是太粗心就能A过去 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #incl