Saving James Bond - Easy Version - BFS遍历

前言

图的遍历,只是这个图有些地方需要自己手动建立,详解有空再补。
详解补了已经。
PTA的测试结点1死活不过,我也没办法,难顶,但是总体思路是没有毛病的,有发现错误麻烦和我说下....万分感谢

题目

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

思路

本质还是图的遍历,只是刚开始图需要自己建立,以及一些小细节。
将所有的鳄鱼的位置和007的位置都视为顶点,并且,显然,是从007的位置开始遍历的。
所有的顶点都应该有这样一种属性,即007是否能从该顶点跳出鳄鱼池,我们暂且称之为逃脱属性吧。
在遍历过程中每到一个新的点就检查该点的逃脱属性,要是能逃脱,那就溜之大吉,如果不能跳出那就输出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;
}

存储结构

就用邻接矩阵吧,挺好的

typedef struct {
	int edges[MaxSize][MaxSize];	//举例小于D则连通
	int n;
	bool isEs[MaxSize];		//逃脱属性
} MGraph;

图的建立

图需要存储的信息有这些:点的信息、点与点之间的关系。
这里点的信息就是上文中的逃脱属性即007能否从该点跳出鳄鱼池,点与点之间的关系就是007能否从这个点跳到那个点。
建立过程就是根据点的属性判断能否连通,以及逃脱属性。
哦对了,顺带一提,这里为了复习C++,用了一个类来存储点的信息,还重载了>>。
还有就是007在岛上,所以第一个结点需要特殊处理下。

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

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

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

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

图遍历

在遍历过程中如果那个点具有逃脱属性,立马跳出,输出Yes。如果所有的点都不具备逃脱属性那就放弃治疗吧,直接放弃吧。
这里选择层序遍历,别问为啥,问就是写起来方便点。

int visit[MaxSize] = { 0, };

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;
}
posted @ 2020-04-23 16:58  游芒。  阅读(219)  评论(0)    收藏  举报