06-图2 Saving James Bond - Easy Version(25 分)

题目描述:

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;

#define Yes 1
#define No  0

struct Node	// 鳄鱼结点
{
	int x, y;	// 记录点的横纵坐标
	int visited;	// 记录点是否被访问
};

int Jump(Node p1, Node p2, int D)	// 计算两点之间的距离是否小于跳跃极限距离
{
	return sqrt(pow((p1.x-p2.x), 2) + pow((p1.y-p2.y), 2)) <= D;
}

int isSafe(Node p, int D)	// 计算是否可以安全到岸
{
	return (50-p.x) <= D || (50-p.y) <= D || (50+p.x) <= D || (50+p.y) <= D;
}

int DFS(int x, Node p[], int D)
{
	p[x].visited = 1;	// 访问过的结点设为1
	int answer;		// 保存结果
	if (isSafe(p[x], D))	// 如果当前点已经能安全到岸就令answer=Yes
		answer = Yes; 	
	else	
	{	/* 如果当前点不能安全到岸*/
		for (int i = 0; p[i].x; i++)	// 循环判定条件也可以写p[i].y, 如果i已经超过数组下标,该条件就为假
		{
			if (!p[i].visited && Jump(p[x], p[i], D))
			{
				answer = DFS(i, p, D);
				if (answer == Yes)
					break;
			}
		}
	}
	return answer;
}

int main(int argc, char const *argv[])
{
	int N, D, idx = 0;
	cin >> N >> D;
	Node O;		// 原点坐标
	O.x = 0, O.y = 0, O.visited = 0;
	Node Croc[N];	// 鳄鱼坐标
	int FirstJumpPoint[N];		// 记录第一次能到达的点的坐标
	for (int i = 0; i < N; i++)
	{
		cin >> Croc[i].x >> Croc[i].y;
		Croc[i].visited = 0;
		if (Jump(O, Croc[i], D+7.5))
		{
			FirstJumpPoint[idx++] = i;		// 由于岛的半径为7.5,所以能跳跃的极限距离为D+7.5
		}
	}
	int answer;
	for (int i = 0; i < idx; i++)
	{
		answer = DFS(FirstJumpPoint[i], Croc, D);
		if (answer == Yes)
		{
			cout << "Yes";
			return 0;
		}
	}
	cout << "No";
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值