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:
1 | 14 20 |
Sample Output 1:
1 | Yes |
Sample Input 2:
1 | 4 13 |
Sample Output 2:
1 | No |
分析:
- 根据输入的鳄鱼条数N创建图中的坐标点,用结构体数组表示;
- 根据当前坐标是否可以直接跳到安全的岸边IsSafe;
- 遍历数组所有的点,判断哪些可以完成第一步跳跃FirstJump;
- 完成第一跳后继续深度搜索DFS哪些点可以进行下一跳CanJump;
- 输出是否存在安全跳到岸边的可能。
编码:
1 |
|
小结:
- 所有鳄鱼坐标的结构选择,邻接矩阵,邻接表?这个是用结构体数组,或是二维数组;
- 输入完所有坐标后,开始第一跳时,也需要判断是否可以直接跳到岸边;
- 求几何运算,如平方、开方、绝对值等,用math库。