PTA 06-图2 Saving James Bond - Easy Version (25分)
Posted 我是一个小木匠!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA 06-图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 NN (\le 100≤100), the number of crocodiles, andDD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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 "iostream" #include "math.h" using namespace std; int n, m; #define MINLEN 42.5 struct Pointer { int x; int y; }p[101]; bool answer = false; /* 记录007能否安全逃生~~ */ bool visited[101] = {false}; /* 判断当前点是否被访问过 */ bool isSave(int x) { /* 判断从当前点能否跳到岸上 */ if ((p[x].x - m <= -50) || (p[x].x + m >= 50) || (p[x].y - m <= -50) || (p[x].y + m >= 50)) return true; return false; } bool jump(int x, int y) { /* 判断2个点距离是否在跳跃能力内 */ int p1 = pow(p[x].x - p[y].x, 2); int p2 = pow(p[x].y - p[y].y, 2); int r = m * m; if (p1 + p2 <= r) return true; return false; } bool firstJump(int x) { /* 当007处于孤岛时 第一次可以选择跳的鳄鱼 因为第一次判断能否跳跃的计算方法与后面dfs不相同 所以要单独写 */ int p1 = pow(p[x].x , 2); int p2 = pow(p[x].y , 2); int r = (m+7.5) * (m+7.5); if (p1 + p2 <= r) { return true; } return false; } bool dfs(int x) { /* 深搜 */ visited[x] = true; if (isSave(x)) { answer = true; } for (int i = 0; i < n; i++) { if (!visited[i] && jump(x, i)) /* 没访问过 并且在跳跃能力之内 */ { answer = dfs(i); if (answer == true) break; } } return answer; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> p[i].x >> p[i].y; } if (m >= MINLEN) { /* 可以直接从孤岛上提到岸上 直接输出 */ cout << "Yes" << endl; return 0; } for (int i = 0; i < n; i++) { if (firstJump(i) && !visited[i]) { /* 如果第一次能够跳的 并且之前没有访问过的节点 则深搜该节点 */ if (dfs(i)) break; } } if (answer == true) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
以上是关于PTA 06-图2 Saving James Bond - Easy Version (25分)的主要内容,如果未能解决你的问题,请参考以下文章
06-图2 Saving James Bond - Easy Version (25 分)
06-图2 Saving James Bond - Easy Version(25 分)
pta 编程题16 Saving James Bond - Easy Version