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
看了一个小时的图应用,回来做了这道题,思路是给所有鳄鱼放在一个数组里面,先从岸上007能够跳到的鳄鱼开始遍历,代码基本都写了注释,但是提交的时候有三个错误,我也很无奈,找了半天不知道哪错了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
#include<iostream> #include<cmath> #include<queue> using namespace std; //用数据结构体存储鳄鱼坐标等信息,dfs遍历则是从数组下标为0开始,,我感觉这样的算法效率太低了,应该可以先根据007的跳跃距离 //给所有的鳄鱼连通起来,这样形成一个个连通图,判断这些连通图能否到达岸边,或者我的dfs遍历不从数组下标为0开始遍历,而是修改 //判断的条件,我觉得都能提高效率把。 #define maxsize 105 typedef struct node { int a;//横坐标 int b;//纵坐标 bool safe;//是否能跳上岸 bool jump;//是否能跳上去 }fishcash; fishcash g[maxsize]; bool visited[maxsize] = { 0 };//标记数组 int n, d;//鳄鱼的数量和跳跃的最大距离 //计算鳄鱼和岸边的距离 bool fishband(int x, int y) { if (abs(x - 50) <= d || abs(x + 50) <= d || abs(y - 50) <= d || abs(y + 50) <= d) return true; return false; } //判断是否能上岸 void issafe() { for (int i = 0; i < n; i++) { if (fishband(g[i].a, g[i].b)) g[i].safe = true; else g[i].safe = false; } } //计算两点间的距离 double getlen(int x1, int x2, int x3, int x4) { return sqrt(pow(x1 - x3, 2.0) + pow(x2 - x4, 2.0)); } //判断能否跳到鳄鱼 void isjump() { for (int i = 0; i < n; i++) { if (getlen(g[i].a, g[i].b, 0, 0) <= d + 7.50) g[i].jump = true; else g[i].jump = false; } } void dfs(int v,bool has) { int i; //判断条件 if (g[v].safe) { has = true; return; } visited[v] = 1; for ( i = 0; i < n; i++) {//这个扩展方式实在太糟糕了! if (visited[v] == 0 && getlen(g[v].a, g[v].b, g[i].a, g[i].b)<=d) dfs(i,has); } } //void bfs(int v, bool has) { // queue<struct node> q; // struct node temp; // visited[v] = true; // q.push(g[v]); // while (!q.empty()) { // temp = q.front(); // q.pop(); // if (temp.safe) { // has = true; // return; // } // for (int i = 0; i < n; i++) { // if (visited[v] == 0 && getlen(temp.a, temp.b, g[i].a, g[i].b)<=d) { // visited[i] = true; // q.push(g[i]); // } // } // } // //} int main() { int tmpx, tmpy; cin >> n >> d; for (int i = 0; i < n; i++) { cin >> tmpx >> tmpy; g[i].a = tmpx; g[i].b = tmpy; } //接下来对鳄鱼的safe和jump进行初始化 issafe(); isjump(); //对所有的鳄鱼(007能够一步跳上去的)进行遍历 bool has = false; for (int i = 0; i < n; i++) { if (g[i].jump) { dfs(i,has); } } if (has) cout << "Yes" << endl; else cout << "No" << endl; } |