05-图2. Saving James Bond - Easy Version (25)
Posted lxjshuju
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了05-图2. Saving James Bond - Easy Version (25)相关的知识,希望对你有一定的参考价值。
05-图2. 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 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; }
题目链接:http://www.patest.cn/contests/mooc-ds/05-%E5%9B%BE2
以上是关于05-图2. Saving James Bond - Easy Version (25)的主要内容,如果未能解决你的问题,请参考以下文章
06-图2 Saving James Bond - Easy Version (25 分)
06-图2 Saving James Bond - Easy Version(25 分)