拯救007成功,不知道我能不能拯救我自己

距离计算最后使用的是平方,如果两个点之间距离小于007能跳的距离就有边相连
节点的datatype为鳄鱼是否能上岸,使用BFS进行遍历,先去能去的鳄鱼身上
每到一个鳄鱼身上就判断一下能不能成功

其实时间空间造成了浪费,因为我走了007的去的每个鳄鱼,然而走到可以上岸的鳄鱼停就行,但我不知道怎么停

其实这道题一开始没思路,只是慢慢分析最后找到了思路.看来我如果耐得住心思也挺强

等下去了解一下什么是贪心算法
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 whether or not he can escape.

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, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12

Sample Output 1:

Yes

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

No

 

/*此题:距离计算最后使用的是平方,如果两个点之间距离小于007能跳的距离就有边相连
节点的datatype为鳄鱼是否能上岸,使用BFS进行遍历,先去能去的鳄鱼身上
每到一个鳄鱼身上就判断一下能不能成功.*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int Flag=0;
#define TRUE 1
#define FALSE 0
typedef struct GNode* PtrToGNode;
typedef struct ENode* PtrToENode;

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;
	struct Coordinate* Coordinate;
};

typedef PtrToENode Edge;
typedef PtrToGNode MyGraph;
MyGraph CreateGraph(int VertexNum);
MyGraph Build_Graph(int VertexNum, int Length);
_Bool Visited[100];
void DFS(MyGraph Grapth, Vertex V);
void UnconnectedDFS(MyGraph Graph, int Length);
int min4(int a, int b, int c, int d);
int main()
{
	int VertexNum, Length;
	scanf("%d %d", &VertexNum, &Length);
	MyGraph a = Build_Graph(VertexNum,Length);
	UnconnectedDFS(a, Length);
	if (Flag == 1)
		printf("Yes");
	else
		printf("No");


}
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++)
		{
			a->G[i][j] = 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;

}

void DFS(MyGraph Graph, Vertex V)
{

	Visited[V] = TRUE;
	if (Graph->Data[V] == 1)
	{
		Flag = 1;
	}

	for (int i = 0; i < Graph->Nv; i++)
	{
			if (Graph->G[V][i] == 1 && Visited[i] == FALSE)
				DFS(Graph, i);
	}


}


void UnconnectedDFS(MyGraph Graph,int Length)
{
	for(int j=0;j<Graph->Nv;j++)
	{
		if ( ((pow(Graph->Coordinate[j].x,2)+pow(Graph->Coordinate[j].y, 2))<pow(Length+7.5,2))&&Visited[j] == FALSE)
		{

			DFS(Graph, j);


		}
	}
}
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;

}

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值