題目地址
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 100≤100), 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 0struct 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\n",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("\n");}}
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");elseprintf("No");
// DBG_ShowStatus(N);
}