07-图5 Saving James Bond - Hard Version

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 (\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

这道题相比之前的拯救007来说,具体有以下变动:
1.输出最短跳数逃生方法;
2.要输出跳数,和踩着的鳄鱼的节点,如果不存在,输出0;
3.如果有多个跳数一样的,输出第一跳距离最短的。

所以考虑用变量:
cnt来计算有多少条路径;
first[]矩阵记录每条路径第一跳的大小。

在简单版本中,利用深度优先或者广度优先都能进行判断,但若要输出最短的逃出路径,深度优先显然不合适了,

因为它是一条道走到黑的…

#include
#include
#include 


#define MAXN 150 //最多150个点 
int dist[MAXN] ;//记录s到w的最短距离 
int cnt = 0;//计算有多少条路径 
int first[MAXN] = {0};//记录每条路径第一跳的大小
int trans_temp = 0;//传送成功了的跳数的第一跳大小 
int jump_path[MAXN] ;//记录每种路径最后一跳的节点 
int path[MAXN];//路径中经过的节点 
int trans_i = 0;
 
//建立邻接矩阵 
struct port{
	int x;
	int y;
}G[MAXN];//坐标 

int NV;//节点数 
int distance;//007能跳的距离

bool jump(int v1,int v2);//计算007一跳能不能成功
bool issafe(int v);//从v点能否跳上岸
bool Unweighted(int v);//无权图单源最短路算法 
bool FirstJump(int v);//第一跳的可能
void save007_hard();//007 主程序
void initialzation();//初始化模块 


int main()
{
	//freopen("in.txt","r",stdin);

	int i;
	int x,y;
	scanf("%d %d",&NV,&distance);//输入的是鳄鱼数
	
	if(distance >= 50-7.5)
	{
		printf("1\n");
		return 0;
	}
	
	G[0].x = G[0].y = 0;//孤岛
	 
	int j = 0;//用来计算在岸上和在岛上的鳄鱼 
	
	for( i=1; i<=NV; i++) //令孤岛为0节点 
	{
		scanf("%d %d",&x,&y);//读入坐标	
		
		if( (7.5*7.5) >= (x*x+y*y) )//鳄鱼在岛上 
		{
			j++;
			i--;
			NV--;
		}
		else if( (abs(x) >= 50 ) || ( abs(y) >= 50 ) )//鳄鱼在岸上 
		{
			j++;
			i--;
			NV--;
		}
		else
		{
			G[i].x = x;
			G[i].y = y; 
		} 


	}
	initialzation();
	save007_hard();
	
	//freopen("out.txt","w",stdout);
	return 0;
} 
//初始化
void initialzation()
{
	int i;
	for( i=0; i= 50) || (y+distance >= 50))
	{
		answer = true;
	}
	else
	{
		answer = false;
	}
	
	return answer;
}

无权图单源最短路算法,针对007这道题 
bool Unweighted(int v)
{
	const int MAXNUM = 200;
	int queue[MAXNUM] = {0};
	int first1 = -1,rear = -1;
	
	queue[++rear] = v;//入队
	
	bool answer = false;//最后一跳节点 


	while(first1 < rear)
	{	
		int de = queue[++first1];//弹出节点 
		
			
		if(issafe(de))//如果此节点可以跳到岸上 
		{
			jump_path[cnt] = de; 
			cnt++;//路径多了一个 
			answer = true;
		} 
		else//否则,访问de的所有邻接点 
		{
			int w;
			for( w=1; w<=NV; w++)
			{		
				if( dist[w] == -1 && jump(de,w) )//de没有被访问过且可以跳过去 
				{
					trans_i = w;//记录每个节点第一跳的长度 
					first[trans_i] = trans_temp; 
					dist[w] = dist[de] +1;//跳数增加 
					path[w] = de;//路径记录 
					queue[++rear] = w;	
				}
			}
	
		} 
	} 

	return answer; 
} 


//第一跳的可能 
bool FirstJump(int v)
{
	int x,y;
	double d;
	bool answer;
	
	
	x = abs(G[v].x);
	y = abs(G[v].y);//用绝对值 
	d =sqrt(pow(x,2) + pow(y,2));
	
	if( d <= (distance+7.5) )//小岛半径7.5,直径15 
	{
		trans_temp = d;  //记录第一跳的距离 
		answer = true;
	}
	else
	{
		answer = false;
	}
	
	return answer; 
}
//007 主程序
void save007_hard()
{
	int i,node;
	
	dist[0] = 0;//设孤岛为0节点,跳数为0 
	
	node = Unweighted(0);//输出最后一个节点
		

	if(node == true)//说明有节点可以跳到岸上 
	{
		//简单建立一个堆栈 
		int stack[100] = {0},top = 0;
		int min_dist,dist_index;
		if(cnt != 1)
		{
		//判断多个最短路径跳数
			min_dist = dist[jump_path[0]];
			dist_index = 0;
			
			for( i=1; i


目前测试点4和测试点5通不过,测试点5我知道是相同跳数,第一跳最短的情况,但是没想好怎么处理

测试点4感觉处理了,但是通不过,不知道哪里没考虑到,先放到这里





你可能感兴趣的:(网易云课堂-陈越,何钦铭-数据结构-2016春)