07-圖5 Saving James Bond - Hard Version   (30分)

This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).

Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.

Input Specification:

Each input file contains one test case. Each case starts with a line containing two positive integers NN (100), the number of crocodiles, and DD, the maximum distance that James could jump. Then NN lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.

Output Specification:

For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.

Sample Input 1:

17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10

Sample Output 1:

4
0 11
10 21
10 35

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

0

思路:

1、首先是判斷鄰接點的問題,用FirstJump和Jump來判斷。相似題:Saving James Bond - Easy Version  

2、核心算法:無權圖的單源最短路徑算法(ShortestPath函數)


注意點:

1、Path在每做一次單源最短路都會更新一次,因此在需要的情況下,要用一個堆棧來把這個路徑記錄一下

2、做最短路時,每次對出隊的鱷魚判斷是否能直接跳到岸邊,如果能就直接返回這個鱷魚

3、第一次跳的時候,岸上的鱷魚不跳

4、如果有多條最短路,選擇第一跳最近的。因此先將所有鱷魚根據離中心點(0,0)的距離來從小到大排序。

5、007的跳躍能力超過或等於42.5  ( 50 - 7.5 ),可以直接跳上岸

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#define MAX 105
using namespace std;
int N, D;
int Visited[MAX], dist[MAX], path[MAX];
struct Crocodile {
	int x, y;
	double d;
} cro[MAX];
double ComputeDistance( int a, int b ) {
	double x = abs( cro[a].x - cro[b].x ); 
	double y = abs( cro[a].y - cro[b].y );
	return sqrt( x * x + y * y );
}
int FirstJump( struct Crocodile dot ) {
	double temp = dot.x * dot.x + dot.y * dot.y;
	double dis = sqrt( temp );
	double cap = 7.5 + D;	//第一次跳要加上小島的半徑
	if( cap >= dis && dis > 7.5) return 1;	//岸上的鱷魚不算
	else return 0;
}
int Jump( int a, int b ) {
	double dis = ComputeDistance(a, b);
	if( D >= dis) return 1;
	else return 0;
}
//v點是否能直接跳上岸邊
int IsSafe( int v ) {
	int x = abs( cro[v].x );
	int y = abs( cro[v].y );
	if( x + D >= 50 || y + D >= 50 )
		return 1;
	else return 0;
}
int ShortestPath(int S) {
	for(int i = 0; i < MAX; i++)
		dist[i] = path[i] = -1;
	int V;
	queue<int> q;
	Visited[S] = 1;
	dist[S] = 0;
	if( IsSafe(S) ) return S; //007跳2次就能上岸(經過1條鱷魚)
	q.push(S);
	while( !q.empty() ) {
		V = q.front();
		q.pop();
		for(int i = 0; i < N; i++){
			if( !Visited[i] && Jump(V, i) ){
					dist[i] = dist[V] + 1;
					path[i] = V;
					Visited[i] = 1;
					if( IsSafe(i) ) return i;
					q.push(i);
			}
		}
	}
	return -1;
}
void PrintPath( stack<int> s ){
	int temp;
	while( !s.empty() ) {
		temp = s.top();
		s.pop();
		printf("%d %d\n", cro[temp].x, cro[temp].y);
	}
}
void Save007() {
	int NumberOfJump = MAX, LastCro;
	stack<int> s;
	for(int i = 0; i < N; i++) {
		if ( !Visited[i] && FirstJump(cro[i]) ) {
			LastCro = ShortestPath(i);
			if( LastCro != -1 && NumberOfJump > dist[LastCro] ) { //如果有更短的路徑
				NumberOfJump = dist[LastCro];  //更新Jump次數
				//將路徑入棧保存(path每做一次單源最短路就要更新一次,因此要記錄)
				//錯誤原因:沒記錄路徑,直接用了path來輸出,導致無限循環,內存超限
				while ( !s.empty() ) //清空上一次的stack(stack沒有clear方法)
					s.pop();
				int v = LastCro;
				while ( v != i ){
					s.push(v);
					v = path[v];
				}
				s.push(v);
			}
		}
	}
	if (NumberOfJump == MAX) printf("0\n"); //沒有路徑能到岸上
	else {
		printf("%d\n", NumberOfJump + 2); //加上在島上跳的一次,和最後跳上岸的一次
		PrintPath( s );
	}
}
int main(){
	scanf("%d %d", &N, &D);
	for(int i = 0; i < N; i++){
		scanf("%d %d", &cro[i].x, &cro[i].y);
		double temp = cro[i].x * cro[i].x + cro[i].y * cro[i].y;
		cro[i].d = sqrt( temp );
	}
	//按離原點的距離從小到大排序
	for(int i = 0; i < N - 1; i++){
		int step = 0;
		for(int j = 0; j < N - 1 - i; j++)
			if(cro[j].d > cro[j + 1].d){
				struct Crocodile Tmp = cro[j];
				cro[j] = cro[j + 1];
				cro[j + 1] = Tmp;
				step++;
			}
		if ( step == 0 ) break;
	}
	//007有超能力,能直接跳上岸
	if( D >= 42.5 ) 
		printf("1\n");	
	else 
		Save007();
	system("pause");
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章