• 欢迎访问废江's博客` QQ群`
• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏本站吧

# 06-图2 Saving James Bond – Easy Version

2周前 (11-28) 12次浏览

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

```#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;
}
```

[] • 版权声明

本站的文章和资源来自互联网或者站长
的原创，按照 CC BY -NC -SA 3.0 CN
协议发布和共享，转载或引用本站文章
应遵循相同协议。如果有侵犯版权的资
源请尽快联系站长，我们会在24h内删
除有争议的资源。
• 网站驱动

• 友情链接

• 关于我们