06-图2 Saving James Bond - Easy Version
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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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
1 //勾股定理确定两点之间的距离,在007能跳到的最大范围内就跳过去递归地DFS该点 2 //一个点没有能到的点了就返回上一点寻找另一能跳到的点,类似于列出所有连通集 3 //找到一条路径能到岸就能确定是Yes,遍历完所有点都没有能到岸的路径才能确定No 4 #include<stdio.h> 5 #include<math.h> 6 #define max 105 7 #define diameter 15 8 9 bool Visited[max] = {false}; //Visited[]记录该点是否被踩过(遍历过) 10 int N, D; //N:点(鳄鱼)的个数, D: 007能跳的最大距离 11 12 struct Node{ 13 int x; 14 int y; 15 }Cro[max]; 16 17 //是否是007从岛上跳出的第一步 18 bool FirstStep(int i){ 19 int d = pow(Cro[i].x , 2.0) + pow(Cro[i].y , 2.0) ; 20 int maxd = pow((diameter/2) + D , 2.0); //在岛上007可以自行走到到边缘,所以他能到的最远距离是D+岛的半径 21 return d <= maxd; 22 } 23 24 //是否能跳上岸 25 bool ReachLand(int i){ 26 int x = abs(Cro[i].x); 27 int y = abs(Cro[i].y); 28 return ( (x+D>=50) || (y+D>=50)); 29 } 30 31 //能否从i点调到j点 32 bool CanReach(int i, int j){ 33 int d = pow((Cro[j].x - Cro[i].x), 2.0) + pow((Cro[j].y - Cro[i].y), 2.0); 34 int maxd = pow(D, 2.0); 35 return d <= maxd; 36 } 37 38 bool DFS(int i){ 39 Visited[i] = true; 40 if(ReachLand(i)) return true; //如果该点能到岸,返回true 41 bool flag = false; 42 for(int j=0; j<N; j++){ 43 if(!Visited[j] && CanReach(i, j)){ //如果该点没有去过,且能跳过去 44 flag = DFS(j); //跳过去,并递归地DFS该点 45 } 46 } 47 return flag; 48 } 49 50 void Save007(){ 51 bool flag = false; 52 for(int i=0; i<N; i++){ 53 if(!Visited[i] && FirstStep(i)){ //找到没有去过的,能从岛上跳过去的点 54 flag = DFS(i); //DFS该点 55 } 56 } 57 if(flag){ 58 printf("Yes\n"); 59 } 60 else{ 61 printf("No\n"); 62 } 63 } 64 65 int main( ){ 66 scanf("%d%d", &N, &D); 67 for(int i=0; i<N; i++){ 68 scanf("%d%d", &Cro[i].x , &Cro[i].y ); 69 } 70 Save007(); 71 return 0; 72 }

浙公网安备 33010602011771号