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 12Sample Output 1:
YesSample Input 2:
4 13 -12 12 12 12 -12 -12 12 -12Sample Output 2:
No
#include <stdio.h> #include <math.h> typedef struct { double x, y; }Coord; int reachCenter(double distance, Coord point) { //判断一只鳄鱼是否能从岛上一步跳至 return (15 + distance)*(15 + distance) >= point.x*point.x + point.y*point.y; } int reachBetween(double distance, Coord p1, Coord p2) { //判断两只鳄鱼间能否一步跳到 return distance*distance >= (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y); } int reachBank(double distance, Coord p) { //判断从这个鳄鱼头上能否跳到岸上 return p.x <= -50 + distance || p.x >= 50 - distance || p.y >= 50 - distance || p.y <= -50 + distance; } int DFS(double distance, Coord *croc, int v, int *visited, int n) { //递归进行深度优先搜索,找到路径返回1 if (reachBank(distance, croc[v])) return 1; for (int i = 0; i < n; ++i) if (!visited[i] && reachBetween(distance, croc[v], croc[i])) { visited[i] = 1; if (DFS(distance, croc, i, visited, n)) return 1; } return 0; } int main() { // freopen("test.txt", "r", stdin); int n; double distance; scanf("%d%lf", &n, &distance); if (distance >= 35) { //如果能直接跳到岸上,直接返回结果 printf("Yes\n"); return 0; } Coord croc[102]; //保存鳄鱼的坐标 for (int i = 0; i < n; ++i) scanf("%lf%lf", &croc[i].x, &croc[i].y); int visited[100] = {}; //节点已访问标记 int flag = 0; //是否已找到路径标记 for (int i = 0; i < n; ++i) if (!visited[i] && reachCenter(distance, croc[i])) { //遍历能从岛上一步跳到的鳄鱼 visited[i] = 1; if (DFS(distance, croc, i, visited, n)) { //从当前鳄鱼开始深度优先搜索 printf("Yes\n"); flag = 1; break; } } if (flag == 0) printf("No\n"); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
05-图2. Saving James Bond - Easy Version (25)
原文地址:http://blog.csdn.net/ice_camel/article/details/47062445