5-11 Saving James Bond - Hard Version (30分)

5-11 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 (\le 100100), the number of crocodiles, and DD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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)(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 <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <math.h>
#include <algorithm>  
using namespace std; 
#define MAX 101

typedef struct node{
	int x,y;
}EYU;


int N; 
double D;
const double r = 15/2;
EYU weizhi[MAX];
int path[MAX];
int dist[MAX];

bool firstjump(int i)
{
	return (sqrt(weizhi[i].x * weizhi[i].x + weizhi[i].y * weizhi[i].y)) <= D + r;
}

bool isSave(int v)
{
	return (abs(weizhi[v].x) >= 50 - D || abs(weizhi[v].y) >= 50 - D);
}

bool jump(int v,int k)
{
	return sqrt( (weizhi[v].x - weizhi[k].x) * (weizhi[v].x - weizhi[k].x) + 
	(weizhi[v].y - weizhi[k].y) * (weizhi[v].y - weizhi[k].y) ) <= D;
}

bool cmp(int a,int b)
{
	return (weizhi[a].x * weizhi[a].x + weizhi[a].y * weizhi[a].y) < (weizhi[b].x * weizhi[b].x + weizhi[b].y * weizhi[b].y);
}

int BFS(int v)
{
	int dui[N+1],first[N+1];
	int rear = -1,front = -1,flag = 0,i = 0;
	dist[v] = 0;
	/*如果存在多条最短路径,选离第一次跳离鳄鱼最近的一条。*/ 
	for(int i = 0; i < N+1; i++)
	{
		first[i] = i;
	}
	
	sort(first,first + N + 1,cmp);/*将每个鳄鱼的距离从小到大排序后,再依次入队*/
	
	for(i = 1; i < N+1; i++)
	{
		v = first[i];
		double firstdist = sqrt(weizhi[v].x * weizhi[v].x + weizhi[v].y * weizhi[v].y);
		if(firstdist > r && firstdist <= D + r)/*不能跳在岛上的鳄鱼*/
		{
			dui[++rear] = v;
			dist[v] = 1;
			path[v] = 0;/*起跳点是0,v是鳄鱼的位置*/
		}
	}

	while(front < rear)
	{
		v = dui[++front];

		if(isSave(v))/*判断是否到岸*/
		{
			flag = v;/*这里v是最后一条鳄鱼的位置*/
			break;
		}
		else
		{
			for(int i = 1; i < N+1; i++)/*将正踩在这个鳄鱼的所有可跳的下一条鳄鱼位置(未访问)全部入队,这些鳄鱼是当前最外层的鳄鱼*/
			{
				if(dist[i] == -1 && jump(v,i))
				{
					dist[i] = dist[v] + 1;/*每次加一就相当与往外扩了一层,也代表当前鳄鱼是离原点的最短路径(指跳的步数)*/
					path[i] = v;/*记下前一条鳄鱼*/ 
					dui[++rear] = i;
				}
			}
		}
	}
	return flag;
}


int main(void)
{
	int v = 0,flag = 0;
	bool isjump;
	scanf("%d%lf",&N,&D);
	weizhi[0].x = 0;/*起点位置不算作鳄鱼输出*/
	weizhi[0].y = 0;
	for(int i = 1; i <= N; i++)
	{
		scanf("%d%d",&weizhi[i].x,&weizhi[i].y);
	}
	
	for(int i = 0; i < N+1; i++)
	{
		dist[i] = -1;
		path[i] = -1;
	}

	if(D + r >= 50)/*如果一步就可以跳出,直接输出*/ 
	{
		printf("1\n");
		return 0;
	}
	else
	flag = BFS(v);
	
	if(!flag)
	{
		printf("0");
	}
	else
	{
		int a[N],count = 0;
		printf("%d\n",dist[flag] + 1);
		while(flag != 0)
		{
			a[count++] = flag;
			flag = path[flag];
		}
		
		for(int i = count - 1; i >= 0; i--)
		{
			printf("%d %d\n",weizhi[a[i]].x,weizhi[a[i]].y);
		}
	}
	
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值