数据结构PTA习题:07-图5 Saving James Bond - Hard Version (30分)

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.
在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据说当年替身演员被最后一条鳄鱼咬住了脚,幸好穿的是特别加厚的靴子才逃过一劫。)
设鳄鱼池是长宽为100米的方形,中心坐标为 (0, 0),且东北角坐标为 (50, 50)。池心岛是以 (0, 0) 为圆心、直径15米的圆。给定池中分布的鳄鱼的坐标、以及007一次能跳跃的最大距离,你需要给他指一条最短的逃生路径 —— 所谓“最短”是指007要跳跃的步数最少。

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.
输入格式:
首先第一行给出两个正整数:鳄鱼数量 N(≤100)和007一次能跳跃的最大距离 D。随后 N 行,每行给出一条鳄鱼的 (x,y) 坐标。注意:不会有两条鳄鱼待在同一个点上。

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.
输出格式:
如果007有可能逃脱,首先在第一行输出007需要跳跃的最少步数,然后从第二行起,每行给出从池心岛到岸边每一步要跳到的鳄鱼的坐标 (x,y)。如果没可能逃脱,就在第一行输出 0 作为跳跃步数。如果最短路径不唯一,则输出第一跳最近的那个解,题目保证这样的解是唯一的。

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

这时一个无权图多源最短路径问题。我用的方法是对多个顶点重复调用Dijkstra算法求单源最短路径,最后找出其中的最小值。
我用的邻接表存储图。
首先将所有鳄鱼形成一张无权图,两只鳄鱼之间距离≤D,表示这两顶点直接相连。
dist[i]存储顶点i到各顶点的最短距离。
为了不让后面的路径覆盖前面的路径,path我用了二维数组存储,从顶点i开始起跳,path[i][]存储各父顶点下标。
最后输出路径的时候用path数组从最后一跳回溯到第一跳,用堆栈的“后进先出”的特点实现从第一跳到最后一跳顶点的输出。
PS:注意题目里直径是15米,而不是半径15米。

C语言实现:(我写的代码有些长。。。)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
struct vertex //顶点是结构体,代表鳄鱼的坐标
{
	int x;
	int y;
};
typedef struct vertex Vertex;
struct edge
{
	int ve; //下标值
	struct edge * NextEdge;
};
typedef struct edge * Edge;
struct gnode
{
	int firstnode; //下标值
	struct edge * firstedge;
};
typedef struct gnode * Gnode;
struct graph //邻接表存储图
{
	int Nv;
	int Ne;
	Gnode M;
};
typedef struct graph * Graph;
struct que //队列
{
	int front;
	int rear;
	int * dui;
};
typedef struct que * Queue;
struct stack //堆栈
{
	int top;
	int * zhan;
};
typedef struct stack * Stack;
Queue CreateQueue(int N); //创建队列,队列存储的是顶点下标
void AddQ(Queue Q, int v); //向队列内插入元素
int DeleteQ(Queue Q); //弹出队列内的元素
Stack CreateStack(int N); //创建堆栈,堆栈内存储的是顶点下标
void Push(Stack S, int v); //压栈
int Pop(Stack S); //出栈
Graph CreateGraph(int N); //创建图
double distance(int x1, int y1, int x2, int y2); //求(x1,y1)和(x2,y2)两点间的距离
int * Visited; //Visited数组判断顶点是否被访问
int main()
{
	int N, D;
	scanf("%d %d", &N, &D);
	if (D >= 50 - 7.5) { printf("%d\n", 1); }//只需跳一次的情况。注意湖心岛直径15,而非半径
	else
	{
		Graph G;
		G = CreateGraph(N);
		int i, j;
		Vertex * v; //将所有输入的顶点依次排入数组v中,每个顶点对应数组内的一个下标
		v = (Vertex *)malloc(N * sizeof(Vertex));
		for (i = 0; i < N; i++)
		{
			scanf("%d %d", &(v[i].x), &(v[i].y));
			G->M[i].firstnode = i;
		}
		//将所有鳄鱼顶点创建成一张无向图,用邻接表存储
		//两只鳄鱼之间距离<=D,则这两顶点直接相连
		for (i = 0; i < N; i++)
		{
			for (j = 0; j < N; j++)
			{
				if (i == j) { continue; }
				if (distance(v[G->M[i].firstnode].x, v[G->M[i].firstnode].y, v[j].x, v[j].y) <= D)
				{
					Edge e;
					e = (Edge)malloc(sizeof(struct edge));
					e->ve = j;
					e->NextEdge = G->M[i].firstedge;
					G->M[i].firstedge = e;
				}
			}
		}
		//调试,依次输出各个顶点的邻接表
		/*
		for (i = 0; i < N; i++)
		{
			printf("%d %d   ", v[G->M[i].firstnode].x, v[G->M[i].firstnode].y);
			Edge p;
			p = G->M[i].firstedge;
			while (p != NULL)
			{
				printf("%d %d   ", v[p->ve].x, v[p->ve].y);
				p = p->NextEdge;
			}
			printf("\n");
		}
		*/
		//
		int * * path, *dist;//path数组存储父顶点,dist数组存储无向图最短距离
		//为了不让后面的路径覆盖前面的路径,path数组我用了二维数组存储,对每个顶点下标i开始起跳,对应path[i][]存储父顶点
		path = (int **)malloc(N * sizeof(int*));
		for (i = 0; i < N; i++)
		{
			path[i] = (int *)malloc(N * sizeof(int));
		}
		dist = (int *)malloc(N * sizeof(int));
		for (i = 0; i < N; i++) //path数组和dist数组的初始化
		{
			for (j = 0; j < N; j++)
			{
				path[i][j] = -1;
			}
			dist[i] = -1;
		}
		int *min, *index; 
		//min[i]存储从顶点i起跳到岸边的最短距离,min[i]=-1表示从顶点i起跳最后到不了岸边
		//index[i]存储跳到岸边前的最后一个顶点的下标
		min = (int *)malloc(N * sizeof(int));
		index = (int *)malloc(N * sizeof(int));
		for (i = 0; i < N; i++) //min数组和index数组初始化
		{
			min[i] = -1;
			index[i] = -1;
		}
		//对每个能起跳的顶点分别使用Dijkstra算法,得到各自的最短距离和最短路径存于dist数组和path数组
		//对无向图情况,Dijkstra算法思路和广度优先搜索BFS类似
		int x; 
		Edge p;
		for (i = 0; i < N; i++)
		{
			if (distance(v[G->M[i].firstnode].x, v[G->M[i].firstnode].y, 0, 0) <= D + 7.5)
			{
				Visited = (int *)malloc(N * sizeof(int));
				for (j = 0; j < N; j++)
				{
					Visited[j] = 0;
				}
				Queue Q;
				Q = CreateQueue(N);
				dist[i] = 0; Visited[i] = 1;
				if (v[G->M[i].firstnode].x + D >= 50 || v[G->M[i].firstnode].x - D <= -50 || v[G->M[i].firstnode].y + D >= 50 || v[G->M[i].firstnode].y - D <= -50)
				{
					min[i] = dist[i];
					index[i] = i;
				}
				else
				{
					AddQ(Q, i);
					int end = 0;
					while (Q->front != Q->rear)
					{
						x = DeleteQ(Q);
						p = G->M[x].firstedge;
						while (p != NULL)
						{
							if (Visited[p->ve] == 0)
							{
								AddQ(Q, p->ve);
								dist[p->ve] = dist[x] + 1;//距离比前一个顶点(父顶点)加1
								path[i][p->ve] = x;//父顶点
								Visited[p->ve] = 1;
								if (v[p->ve].x + D >= 50 || v[p->ve].x - D <= -50 || v[p->ve].y + D >= 50 || v[p->ve].y - D <= -50)//某顶点能到岸边,则从顶点i起跳情况计算完毕
								{
									min[i] = dist[p->ve];
									index[i] = p->ve;
									end = 1;
									break;
								}
							}
							p = p->NextEdge;
						}
						if (end == 1) { break; }
					}
				}
			}
		}
		int yes = 0; int m = 10000; int t = 0;//m记录所有顶点情况的最短距离最小值,t记录该起跳顶点下标值
		for (i = 0; i < N; i++)
		{
			if (min[i] != -1)
			{
				yes = 1;
				if (min[i] >= 0 && min[i] < m)//min[i]更小,更新m和t值
				{
					m = min[i];
					t = i;
				}
				else if (min[i] >= 0 && min[i] == m)//相等的情况取第一跳最近的情况
				{
					if (distance(v[i].x, v[i].y, 0, 0) < distance(v[t].x, v[t].y, 0, 0))
					{
						t = i;
					}
				}
			}
		}
		if (yes == 0) { printf("0"); }//如果从任何顶点起跳的情况中min[i]值均为-1,则无法上岸
		else
		{
			printf("%d\n", m + 2);//由于dist数组元素最小值为0,所以要加2输出
			Stack S = CreateStack(N);
			i = index[t];//t是最小情况的起跳顶点下标值。index[t]是跳到岸边前最后一个顶点下标值
			//通过堆栈从第一跳输出到最后一跳,通过path数组从最后一跳回溯到第一跳
			while (path[t][i] != -1)
			{
				Push(S, i);
				i = path[t][i];
			}
			printf("%d %d\n", v[i].x, v[i].y);
			while (S->top != -1)
			{
				int r = Pop(S);
				printf("%d %d\n", v[r].x, v[r].y);
			}
		}
	}
	return 0;
}
Graph CreateGraph(int N) //创建图
{
	Graph G;
	G = (Graph)malloc(sizeof(struct graph));
	G->Nv = N;
	G->Ne = 0;
	G->M = (Gnode)malloc(N * sizeof(struct gnode));
	int i;
	for (i = 0; i < N; i++)
	{
		G->M[i].firstedge = NULL;
	}
	return G;
}
double distance(int x1, int y1, int x2, int y2) //求(x1,y1)和(x2,y2)两点间的距离
{
	double d;
	d = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
	return d;
}
Queue CreateQueue(int N) //创建队列,队列存储的是顶点下标
{
	Queue Q;
	Q = (Queue)malloc(sizeof(struct que));
	Q->front = Q->rear = -1;
	Q->dui = (int *)malloc(N * sizeof(int));
	return Q;
}
void AddQ(Queue Q, int v) //向队列内插入元素
{
	Q->rear++;
	Q->dui[Q->rear] = v;
}
int DeleteQ(Queue Q) //弹出队列内的元素
{
	Q->front++;
	return Q->dui[Q->front];
}
Stack CreateStack(int N) //创建堆栈,堆栈存储的是顶点下标
{
	Stack S;
	S = (Stack)malloc(sizeof(struct stack));
	S->top = -1;
	S->zhan = (int *)malloc(N * sizeof(int));
	return S;
}
void Push(Stack S, int v) //压栈
{
	S->top++;
	S->zhan[S->top] = v;
}
int Pop(Stack S) //出栈
{
	return S->zhan[S->top--];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值