06-图2 Saving James Bond - Easy Version (25 分) 图遍历算法的使用

在《生死逃亡》这一幕中,詹姆斯·邦德被一群毒枭困于湖中心的鳄鱼岛上。他利用最大跳跃能力成功地从一只鳄鱼跳到另一只,最终在最后一刻抵达湖岸逃生。此问题转化为图论问题,通过建立邻接矩阵和深度优先搜索算法,判断邦德能否从鳄鱼岛出发到达湖岸。在给定的坐标和跳跃距离条件下,程序成功模拟了这一过程并得出结论。
摘要由CSDN通过智能技术生成

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

这题其实挺简单的,就是一个图遍历算法的使用DFS或者BFS都可以。
首先就是把圆盘当作图的一个点,把鳄鱼当作图的其他点。判断从圆盘上可不可以跳到鳄鱼身上。然后判断鳄鱼直接的距离,看看是否满足跳跃条件,然后再把这些满足跳跃条件的点连接起来,构成了一个图。
然后使用DFS或者BFS遍历,把可以走到的点的Visited[i]变成1,最后判断满足visited[i]=1的点是否满足上岸条件(即到岸边距离小于跳跃距离)。这样就可以得到答案。
以下是代码:

#include<iostream>
#include<cmath>
using namespace std;
const int MaxSize = 101;
int A[MaxSize][MaxSize] = { 0 };
typedef int ElemType;
typedef struct Pnode {
	double x;
	double y;
}Point;
//求两点之间距离
double distanc(Point a, Point b)
{
	double x1 = a.x - b.x, y1 = a.y - b.y;
	return sqrt(x1 * x1 + y1 * y1);
}
//首先建立邻接表
typedef struct ANode {
	int adjvex;//边结点编号
	struct ANode* nextarc;//指向下一个边结点
}ArcNode;//边结点类型
typedef struct node {
	ElemType data;
	ArcNode* firstarc;//注意,这里的头结点也是边结点类型,这样才能在同一条链表
}VNode;//头结点类型
typedef struct Node {
	VNode adjlist[MaxSize];
	int n, e;
}AdjGraph;//邻接表类型
//创建图的算法
void CreateAdj(AdjGraph*& G, int A[MaxSize][MaxSize], int n)
{
	int i, j;
	ArcNode* p;
	G = new AdjGraph;
	for (int temp = 0;temp < n;++temp)
		G->adjlist[temp].firstarc = NULL;
	for (i = 0;i < n;++i)
		for (j = n - 1;j >= 0;--j)
		{
			if (A[i][j] != 0)
			{
				p = new ArcNode;
				p->adjvex = j;
				p->nextarc = G->adjlist[i].firstarc;
				G->adjlist[i].firstarc = p;
			}
		}
	G->n = n;
}
//DFS算法
int Visited[MaxSize] = { 0 };
void DFS(AdjGraph* G, int v)
{
	ArcNode* p;
	Visited[v] = 1;
	p = G->adjlist[v].firstarc;
	while (p != NULL)
	{
		if (Visited[p->adjvex] == 0)
			DFS(G, p->adjvex);
		p = p->nextarc;
	}
}
int main()
{
	int N;
	double D;
	cin >> N >> D;
	Point point[MaxSize];
	point[0].x = 0, point[0].y = 0;
	for (int i = 1;i <= N;++i)
		cin >> point[i].x >> point[i].y;
	//把原点和可以跳到的鳄鱼点连接
	for (int i = 1;i <= N;++i)
		if (distanc(point[0], point[i]) <= D + 7.5)
		{
			A[0][i] = 1;
			A[i][0] = 1;
		}
	//把可以进行跳跃的鳄鱼点进行连接
	for(int i=1;i<=N;++i)
		for(int j=1;j<=N;++j)
			if (distanc(point[i], point[j]) <= D && i != j)
			{
				A[i][j] = 1;
				A[j][i] = 1;
			}
	//判断该点是否可以直接上岸
	bool ans[MaxSize] = { false };
	for (int i = 1;i <= N;++i)
	{
		if (abs(point[i].x - 50) <= D || abs(point[i].x + 50) <= D || abs(point[i].y - 50) <= D || abs(point[i].y + 50) <= D)
			ans[i] = true;
	}
	AdjGraph* G;
	CreateAdj(G, A, N + 1);
	DFS(G, 0);
	int flag = 0;
	for (int i = 1;i <= N;++i)
	{
		if (ans[i] == true && Visited[i] == 1)
		{
			flag = 1;
			cout << "Yes" << endl;
			break;
		}
	}
	if (!flag)
		cout << "No" << endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值