PTA-07-图5 Saving James Bond - Hard Version

题目描述

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 N (≤100), the number of crocodiles, and D, the maximum distance that James could jump. Then N 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


#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值