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

06-图2 Saving James Bond – Easy Version

浙大mooc 站点默认 2周前 (11-28) 12次浏览 已收录 0个评论

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

不知道大家有没有和我一样的感觉,好像知道怎么写,却又无从下手,我也是在这题耗了一个多小时感觉还是无从下手,题意我就不说了,不会英文的小朋友百度翻译一下,一看就能看懂了。我的思路是:给了我n只鳄鱼的位置,这些鳄鱼就如图中的点,遍历图的点需要一个起点,起点就是007从岛上可以跳过去的鳄鱼,所以先找出所有的起点,再从这些起点开始遍历,当我们发现,007可以跳到岸上的时候就结束遍历,输出yes,好像逻辑一整,也不是很难,也许是我代码写的少,或者是图学的还不够熟练把,我就是写不出来。所以,我决定把教材上的练习过一遍,图的遍历的应用,也正好给图的遍历那篇博客的图的遍历的应用板块补全,回来我在写这一题,看看是不是豁然开朗。虽然,我不是很厉害的大佬,也不是一个好的博客写主,没有好的文采,但是,我会给你们看我解决问题的一点一滴的过程。(⊙﹏⊙)希望能帮到你们,一起努力

看了一个小时的图应用,回来做了这道题,思路是给所有鳄鱼放在一个数组里面,先从岸上007能够跳到的鳄鱼开始遍历,代码基本都写了注释,但是提交的时候有三个错误,我也很无奈,找了半天不知道哪错了

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

个人博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:06-图2 Saving James Bond – Easy Version
喜欢 (0)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到