PTA 06-图2 Saving James Bond

题目地址

https://pta.patest.cn/pta/test/16/exam/4/question/672

5-10 Saving James Bond - Easy Version   (25分)

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 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, 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


比较简单的一个题,找所有可以起跳的点设为start,可以上岸的点设为finish
然后把在跳跃距离内的点连起来
遍历所有start,如果路径上遇到finish,那么是可以上岸的

/*
评测结果
时间	结果	得分	题目	编译器	用时(ms)	内存(MB)	用户
2017-07-02 00:53	答案正确	25	5-10	gcc	2	1	
测试点结果
测试点	结果	得分/满分	用时(ms)	内存(MB)
测试点1	答案正确	7/7	2	1
测试点2	答案正确	6/6	1	1
测试点3	答案正确	3/3	2	1
测试点4	答案正确	3/3	2	1
测试点5	答案正确	3/3	1	1
测试点6	答案正确	3/3	1	1
*/
#include<stdio.h>
#include<math.h>
#define MAX_CROCODILE 100
#define CENTRAL_ISLAND_R 7.5
#define BORDER 50
#define TRUE 1
#define FALSE 0

struct tCrocoVertex
{
	int x;
	int y;
	int start;
	int finish;
} gNodeTab[MAX_CROCODILE];

int gMatrix[MAX_CROCODILE][MAX_CROCODILE];
int gVisitedFlag[MAX_CROCODILE];
int gCanBeSavedFlag=FALSE;

void FindStartAndFinishNode(int N,int jumpDistance)
{
	int i,distSquare;
	float startDistSquare=(jumpDistance+CENTRAL_ISLAND_R)*(jumpDistance+CENTRAL_ISLAND_R);
	for(i=0;i<N;i++)
	{
		distSquare=(gNodeTab[i].x)*(gNodeTab[i].x)+(gNodeTab[i].y)*(gNodeTab[i].y);
		
		if(distSquare<=startDistSquare)
		{
			gNodeTab[i].start=TRUE;
		}

		gNodeTab[i].finish=	(abs(gNodeTab[i].x-BORDER)<=jumpDistance ||
		    		 		 abs(gNodeTab[i].x+BORDER)<=jumpDistance ||
				  			 abs(gNodeTab[i].y-BORDER)<=jumpDistance ||
							 abs(gNodeTab[i].y+BORDER)<=jumpDistance)?TRUE:FALSE;
	}
}

void BuildMatrix(int N,int jumpDistance)
{
	int i,j;
	int distSquare,jumpDistanceSquare;
	jumpDistanceSquare=jumpDistance*jumpDistance;
	for(i=0;i<N;i++)
		for(j=0;j<i;j++)
		{
			distSquare=	(gNodeTab[i].x-gNodeTab[j].x)*
						(gNodeTab[i].x-gNodeTab[j].x)+
						(gNodeTab[i].y-gNodeTab[j].y)*
						(gNodeTab[i].y-gNodeTab[j].y);
			if(distSquare<=jumpDistanceSquare)
			{
				gMatrix[i][j]=TRUE;
				gMatrix[j][i]=TRUE;
			}
		}
}

void DFS(int x,int N)
{
	if(gCanBeSavedFlag==TRUE)
		return;
	int i;
	if(gNodeTab[x].finish==TRUE)
		gCanBeSavedFlag=TRUE;
	gVisitedFlag[x]=TRUE;
		for(i=0;i<N;i++)
		{
			if(gMatrix[x][i]==TRUE && gVisitedFlag[i]==FALSE)
				DFS(i,N);
		}
}
void DBG_ShowStatus(N)
{
	int i,j;
	for(i=0;i<N;i++)
	{
		printf("ID:%d,x:%d,y:%d,start:%d,finish:%d,visited:%d
",i,gNodeTab[i].x,gNodeTab[i].y,gNodeTab[i].start,gNodeTab[i].finish,gVisitedFlag[i]);
 	}
 	for(i=0;i<N;i++)
	{
		for(j=0;j<N;j++)
			printf("%d ",gMatrix[i][j]);
		printf("
");
	}

}
int main()
{
	int i,N,D;
	scanf("%d %d",&N,&D);
	for(i=0;i<N;i++)
	{
		scanf("%d %d",&gNodeTab[i].x,&gNodeTab[i].y);
	}
	FindStartAndFinishNode(N,D);
	BuildMatrix(N,D);
	
	for(i=0;i<N;i++)
	{
		if(gCanBeSavedFlag==TRUE)
			break;
		if(gNodeTab[i].start==TRUE && gVisitedFlag[i]==FALSE)
			DFS(i,N);
	}
	if(gCanBeSavedFlag==TRUE)
		printf("Yes");
	else
		printf("No");
// 	DBG_ShowStatus(N);
}

  

原文地址:https://www.cnblogs.com/gk2017/p/7141016.html