拯救007困难模式

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 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<stdio.h>
#include<stdlib.h>
#include<math.h>
int Flag = 0;//0跳不上去1代表能跳上去
typedef struct GNode* PtrToGNode;
typedef struct ENode* PtrToENode;
#define INFINITY 9999
typedef int Vertex;
typedef int WeightType;
typedef char DataType;
struct Coordinate {
	int x;
	int y;
};
struct GNode {
	int Nv;
	int Ne;
	WeightType** G;//二维矩阵,元素为边的权重,若为无向图则权重为1
	DataType* Data;//为1借助这个鳄鱼能跳上去
	struct Coordinate* Coordinate;
};

typedef PtrToENode Edge;
typedef PtrToGNode MyGraph;
MyGraph CreateGraph(int VertexNum);
MyGraph Build_Graph(int VertexNum, int Length);
int StartJump(MyGraph Graph, int Length, int* S, int* D);
int min4(int a, int b, int c, int d);
void Floyd(MyGraph Graph, int** Path);
void Read_Path(int** Path, int a, int b, MyGraph Graph);
void Read_Path0(int** Path, int a, int b, MyGraph Graph);//读取从a到b的路径
int main()
{
	int VertexNum, Length;
	int S;
	int D;
	int Jump;
	scanf("%d %d", &VertexNum, &Length);
	MyGraph a = Build_Graph(VertexNum, Length);
	int** Path = (int**)malloc(sizeof(int*) * a->Nv);
	for (int k = 0; k < a->Nv; k++)
	{
		Path[k] = (int*)malloc(sizeof(int) * a->Nv);
	}
	Floyd(a, Path);
	Jump=StartJump(a, Length, &S, &D)+2;
	if (Flag == 0 && (Length + 7.5) < 50)
		printf("%d", 0);
	else if( (Length + 7.5) >= 50)
	printf("%d", 1);
	else
	{
		printf("%d\n", Jump);
		Read_Path(Path, S, D, a);
	}
}
MyGraph CreateGraph(int VertexNum)//创建一个图并将邻接矩阵所有节点都初始化为0
{
	MyGraph a = (MyGraph)malloc(sizeof(struct GNode));
	a->G = (WeightType**)malloc(sizeof(WeightType*) * VertexNum);//G是一个指向指针的指针
	for (int i = 0; i < VertexNum; i++)
	{
		a->G[i] = (WeightType*)malloc(sizeof(WeightType) * VertexNum);//a->G[i]是一个指向int的指针
	}
	for (int i = 0; i < VertexNum; i++)
	{
		for (int j = 0; j < VertexNum; j++)
		{
			if (i != j)
				a->G[i][j] = INFINITY;
			else
				a->G[i][j] = 0;//自己和自己距离为0
		}
	}
	a->Data = (DataType*)malloc(sizeof(DataType) * VertexNum);
	a->Nv = VertexNum;
	a->Ne = 0;
	return a;
}

MyGraph Build_Graph(int VertexNum, int Length)
{
	MyGraph Graph = CreateGraph(VertexNum);
	int Distance;
	int Up, Down, Left, Right;
	Graph->Coordinate = (struct Coordinate*)malloc(sizeof(struct Coordinate) * VertexNum);
	for (int i = 0; i < VertexNum; i++)
	{
		scanf("%d %d", &Graph->Coordinate[i].x, &Graph->Coordinate[i].y);//写入坐标
	}
	for (int i = 0; i < VertexNum; i++)//距离小于等于长度的连通
	{
		for (int j = i + 1; j < VertexNum; j++)
		{
			Distance = pow(Graph->Coordinate[i].x - Graph->Coordinate[j].x, 2) + pow(Graph->Coordinate[i].y - Graph->Coordinate[j].y, 2);//距离计算
			if (Distance <= pow(Length, 2))
			{
				Graph->G[i][j] = 1;
				Graph->G[j][i] = 1;

			}
		}
	}
	for (int i = 0; i < VertexNum; i++)//判断每只鳄鱼能否让007上岸
	{
		Up = abs(50 - Graph->Coordinate[i].y);
		Down = abs(-50 - Graph->Coordinate[i].y);
		Left = abs(-50 - Graph->Coordinate[i].x);
		Right = abs(50 - Graph->Coordinate[i].x);
		if (min4(Up, Down, Left, Right) <= Length)
			Graph->Data[i] = 1;
		else
			Graph->Data[i] = 0;

	}
	return Graph;

}

int min4(int a, int b, int c, int d)
{
	if (a > b)
		a = b;
	if (c > d)
		c = d;
	if (a > c)
		a = c;
	return a;

}
void Floyd(MyGraph Graph,int**Path)
{

	for (int i = 0; i < Graph->Nv; i++)
	{
		for (int j = 0; j < Graph->Nv; j++)
		{
				Path[i][j] = -1;//-1代表ij直接相连****并不会最后i j不相连引起混淆因为G[i][j]=INFINIFTY

		}
	}
	
	for (int k = 0; k < Graph->Nv; k++)
	{
		for (int i = 0; i < Graph->Nv; i++)
		{
			for (int j = 0; j < Graph->Nv; j++)
			{
				if (Graph->G[i][j] > (Graph->G[i][k] + Graph->G[k][j]))
				{
					Graph->G[i][j] = Graph->G[i][k] + Graph->G[k][j];
					Path[i][j] = k;
				}
			}
		}
	}
}
void Read_Path0(int** Path, int a, int b,MyGraph Graph)//读取从a到b的路径
{

	if (Path[a][b] == -1)
	{
		printf("%d ", Graph->Coordinate[a].x);
		printf("%d\n", Graph->Coordinate[a].y);

	}
		else
		{
			Read_Path0(Path, a, Path[a][b], Graph);
			Read_Path0(Path, Path[a][b], b, Graph);
		}

	
}
void Read_Path(int** Path, int a, int b,MyGraph Graph)
{
	if (a != b)
	{
		Read_Path0(Path, a, b,Graph);
		printf("%d ", Graph->Coordinate[b].x);
		printf("%d", Graph->Coordinate[b].y);

	}
	else
	{
		printf("%d ", Graph->Coordinate[a].x);
		printf("%d", Graph->Coordinate[a].y);

	}

}
int StartJump(MyGraph Graph,int Length,int*S,int*D)
{
	int Jump=INFINITY;//跳几次
	int JumpTemp=INFINITY;
	int STemp;
	int DTemp;
	for (int i = 0; i < Graph->Nv; i++)//每一只鳄鱼
	{
		JumpTemp = INFINITY;
		if ((pow(Graph->Coordinate[i].x, 2) + pow(Graph->Coordinate[i].y, 2)) < pow(Length+7.5,2))//可以从中心跳上这只鳄鱼
		{
			for (int j = 0; j < Graph->Nv; j++)//每一个邻接点(包括自己)
			{
				if( (Graph->Data[j]== 1) && (Graph->G[i][j] != INFINITY))//从这只鳄鱼能跳上岸
				{
					STemp = i;//起点
					Flag = 1;//能跳上去
					if (JumpTemp > Graph->G[i][j])//距离更近
					{
						JumpTemp = Graph->G[i][j];//更新距离
						DTemp = j;
					}
				}
			}
			if (Jump > JumpTemp)
			{
				Jump = JumpTemp;
				*S = STemp;
				*D = DTemp;
			}
			else if (Jump != INFINITY && (Jump == JumpTemp))
			{
				if ((pow(Graph->Coordinate[*S].x, 2) + pow(Graph->Coordinate[*S].y, 2)) >( pow(Graph->Coordinate[STemp].x, 2) + pow(Graph->Coordinate[STemp].y, 2)))
				{
					*S = STemp;
					*D = DTemp;
				}
			}
			else
				;

		}

	}
	return Jump;
}

Floyd算法初始化一开始没搞明白,自己和自己的distance应该为0,path起初都设为-1也没关系,因为到最后也可以判断是否直接相连(distance是否为正无穷)

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值