ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Saving James Bond - Easy Version

2020-04-23 17:00:27  阅读:312  来源: 互联网

标签:p2 12 Saving Point int James p1 MaxSize Version


前言

图的遍历,只是这个图有些地方需要自己手动建立,详解有空再补。
PTA的测试结点2死活不过,我也没办法,难顶,有发现错误麻烦和我说下....

题目

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.

输入格式

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.

输出格式

For each test case, print in a line "Yes" if James can escape, or "No" if not.

样例

输入样例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

输出样例1

Yes

输入样例2

4 13
-12 12
12 12
-12 -12
12 -12

输出样例2

No

实现

完整代码

#include<iostream>
#include<cmath>
using namespace std;

const int MaxSize = 102;
int visit[MaxSize] = { 0, };

typedef struct {
	int edges[MaxSize][MaxSize];	//举例小于D则连通
	int n;
	bool isEs[MaxSize];					//离岸边小于D则在这只鳄鱼上可以联通
} MGraph;

class Point {
public:
	Point() :x(0), y(0) {}
	Point(int xx, int yy) :x(xx), y(yy) {}
	friend istream& operator>>(istream &input, Point &p);			
	void setX(int x) { this ->x = x; }
	void setY(int y) { this->y = y; }
	friend double myDistance(Point p1, Point p2);
	bool isEscape(int D);					//点的属性,应该放在类中
private:
	int x;
	int y;
};

void myBulid(MGraph &g, Point c[], int D);
bool myIsRun(MGraph g);

int main() {
	Point croco[MaxSize];			
	MGraph G;
	int N, D;
	cin >> N >> D;
	//初始化
	G.n = N;
	//先录入鳄鱼的位置,包括007
	//手动输入007的位置,且为起始点
	croco[0].setX(0);
	croco[0].setY(0);
	for (int i = 1; i <= N; i++)
		cin >> croco[i];
	//建立图,以007能跳到表示联通,否则则未联通
	myBulid(G, croco, D);
	//检查007能否跳出,其实就是遍历一遍并在遍历过程中随时检查能否跳出去,能跳出就跳出
	myIsRun(G);
	return 0;
}

istream& operator>>(istream &input, Point &p) {				//最好重载为非成员函数
	input >> p.x >> p.y;
	return input;
}

double myDistance(Point p1, Point p2) {
	return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}

void myBulid(MGraph &g, Point c[], int D) {
	//考虑到中心圆岛的问题,单独为007建立联通关系
	for (int i = 1; i < g.n + 1; i++) 
		if (myDistance(c[0], c[i]) - 7.5 <= D)
			g.edges[0][i] = g.edges[i][0] = 1;
		else
			g.edges[0][i] = g.edges[i][0] = 0;
	//建立鳄鱼中间的联通关系
	for (int i = 1; i < g.n + 1; i++) 
		for (int j = 1; j < i; j++) 		//因为是对称的,减少遍历
			if (myDistance(c[i], c[j]) <= D) 
				g.edges[i][j] = g.edges[j][i] = 1;
			else
				g.edges[i][j] = g.edges[j][i] = 0;
	//自身不能通向自身
	for (int i = 0; i < g.n + 1; i++)
		g.edges[i][i] = 0;
	//检查是否有鳄鱼能跳出
	for (int i = 0; i < g.n + 1; i++) 
		g.isEs[i] = c[i].isEscape(D);
}

bool Point::isEscape(int D) {
	return (50 - abs(x) <= D || 50 - abs(y) <= D);
}

bool myIsRun(MGraph g) {
	int flag = 0;
	//如果从所有的鳄鱼都挑不到岸上,那就肯定无法逃亡了
	for (int i = 1; i < g.n + 1; i++) {
		if (g.isEs[i] == 1) {
			flag = 1;
			break;
		}
	}

	if (flag == 1) {
		int queue[MaxSize];
		int front, rear;
		front = rear = -1;
		queue[++rear] = 0;			//007入队
		flag = 0;
		while (front != rear) {
			int p = ++front;
			//其实就是visit()的内容
			if (g.isEs[p] == 1) {			//能跳出就立马出去
				flag = 1;
				break;
			}
			visit[p] = 1;

			for (int i = 0; i < g.n + 1; i++)
				if (g.edges[p][i] == 1 && visit[i] == 0)
					queue[++rear] = i;
		}
	}

	if (flag == 1)
		cout << "Yes";
	else 
		cout << "No";

	return flag;
}

标签:p2,12,Saving,Point,int,James,p1,MaxSize,Version
来源: https://www.cnblogs.com/Za-Ya-Hoo/p/12761998.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有