本文主要是介绍六、图(上):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 (≤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
代码
#include<stdio.h>
#include<math.h>
#define MaxNum 100
#define Radius 7.5typedef struct Location Cro;
struct Location{int x;int y;
}CroLoc[MaxNum];int Visited[MaxNum] = {0};
int N,D;int FirstJump(Cro C)
{if ( pow(C.x,2) + pow(C.y,2) <= pow(Radius+D,2) )return 1;elsereturn 0;
}int Jump(Cro C1, Cro C2)
{if( pow(C1.x-C2.x,2) + pow(C1.y-C2.y,2) <= pow(D,2) )return 1;else return 0;
}int IsSafe(i)
{int x = CroLoc[i].x, y = CroLoc[i].y;if( x>=50-D || x<=D-50 || y>=50-D || y<=D-50 )return 1;else return 0;
}int DFS(i)
{int j,answer=0;Visited[i] = 1;if( IsSafe(i) )answer = 1;else{for(j=0;j!=N;++j){if( !Visited[j] && j!=i && Jump(CroLoc[i],CroLoc[j]) )answer = DFS(j);if(answer == 1) break;}}return answer;
}int main()
{scanf("%d %d",&N,&D);int i,answer;for(i=0;i!=N;i++)scanf("\n%d %d",&CroLoc[i].x,&CroLoc[i].y);for(i=0;i!=N;i++){if( !Visited[i] && FirstJump(CroLoc[i]) ){answer = DFS(i);if(answer) break;}}if(answer)printf("Yes");elseprintf("No");return 0;}
注意事项
- 因为湖心小岛是有半径的不是一个点,所以第一次跳跃的判断方法和之后的不相同。
- 不需要建立图,直接用距离判断是否连通。
- 使用全局变量方便。先定义全局变量,然后赋值给全局变量。
这篇关于六、图(上):Saving James Bond - Easy Version的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!