A strange lift

简介:

点击打开链接

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

解析:构图,将从i层到按动button后跳转的楼层。看作连通状态,赋值为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;
}






本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5075843.html,如需转载请自行联系原作者


相关文章
UVa11565 - Simple Equations
UVa11565 - Simple Equations
60 0
UVa11714 - Blind Sorting
UVa11714 - Blind Sorting
80 0
LeetCode 407. Trapping Rain Water II
我们把最外面的四边当成四面墙,想象海水面不断的升高,首先会浸过墙面最低的格子,如果墙面最低格子的四周(出了在墙面的格子)有比它矮的格子,那么这就可以形成一个蓄水池,蓄水池的最高高度就是墙面最低的格子,于是我们计算这个蓄水池可以获得的蓄水量;然后这个蓄水池被添加到墙面中;继续在墙面中找最低的格子;
138 0
LeetCode 407. Trapping Rain Water II
|
索引
LeetCode 42 Trapping Rain Water
给定n个非负整数,每个非负整数表示一个宽度为1的柱子,计算下雨后能够捕获多少水.
84 0
LeetCode 42 Trapping Rain Water
|
开发者
牛客第六场-Combination of Physics and Maths
题意:选出一个子矩阵,使得所求的压强最大,压强是指这个子矩阵中每个元素之和 / 这个子矩阵最下面一行的元素之和
71 0
牛客第六场-Combination of Physics and Maths
|
人工智能
Educational Codeforces Round 98 (Rated for Div. 2)B-Toy Blocks
You are asked to watch your nephew who likes to play with toy blocks in a strange way. He has n boxes and the i-th box has ai blocks. His game consists of two steps: he chooses an arbitrary box i; he tries to move all blocks from the i-th box to other boxes.
275 0
|
算法 Go
HDU-1548,A strange lift(Dijkstra)
HDU-1548,A strange lift(Dijkstra)
|
人工智能
poj2891:Strange Way to Express Integers
题目连接: 分明$excrt$就过了。 为什么还要仔细读题呢?    $qwq$ 反正我没读题然后被卡$long \ long +$输出格式错$……$总共$WA$了四次 怕不是要退役…… 上代码:   #include #include #include using na...
1048 0
|
C++ 计算机视觉 Python
Finding distance between two curves
http://answers.opencv.org/question/129819/finding-distance-between-two-curves/ 问题: Hello, Im trying to add tangents along the curve in the image below, like the red lines in the second picture.
1007 0
LeetCode - 42. Trapping Rain Water
42. Trapping Rain Water  Problem's Link  ---------------------------------------------------------------------------- Mean:  在坐标上给你一些竖直放置的条形积木,问你这个积木能够容纳多少液体.
910 0