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

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

解题思路:

总体算法和DFS算法的思路和伪代码如下:

  1. 需要注意的是,由于孤岛本身直径为15,所以第一次跳跃的半径最值为m+7.5;
  2. 可以不使用邻接矩阵或者邻接表来表示图,因为每个点的邻接点的确定,只需判断所有顶点中是否在跳跃能力为半径的圆内即可,且由于顶点个数的规模不大,涉及的计算量也不大,可以不对图进行存储;
  3. 在DFS的实现中,递归前需判断当前点是否可以到岸,若可以,跳出DFS函数,否则进行递归。

代码如下:

#include <iostream>
#include <math.h>

using namespace std;
int n, m; //n为鳄鱼个数,m为詹姆斯跳跃的最大距离

constexpr auto MINLEN = 42.5; //MINLEN表示小岛到岸边的距离;

struct Point {
	int x;
	int y;
}Pnt[101];

bool answer = false; /*记录詹姆斯能否逃脱*/
bool visited[101] = { false }; /*记录当前点是否被访问过*/

bool isSave(int serial) { /*判断从当前点是否可以跳到岸上*/
	if (Pnt[serial].x - m <= -50 || Pnt[serial].x + m >= 50
		|| Pnt[serial].y + m >= 50 || Pnt[serial].y - m <= -50)
		return true;
	return false;
}

bool jump(int pnt1, int pnt2) { /*判断2个点距离是否在跳跃能力内*/
	/*int len_X = pow(Pnt[pnt1].x - Pnt[pnt2].x, 2);
	int len_Y = pow(Pnt[pnt1].y - Pnt[pnt2].y, 2);
	int distance = len_X + len_Y;
	if (distance <= m ^ 2)
		return true;
	return false;*/
	return (pow(Pnt[pnt1].x - Pnt[pnt2].x, 2) + pow(Pnt[pnt1].y - Pnt[pnt2].y, 2)) <= pow(m, 2) ? true : false;
}

bool firstJump(int pnt) { 
	/*当007处于孤岛时,第一次可以选择跳的鳄鱼*/
	/*第一次判断能否跳跃可以以小岛的为跳板,所以跳跃能力多出7.5的距离*/
	/*int len_X = pow(Pnt[pnt].x, 2);
	int len_Y = pow(Pnt[pnt].y, 2);
	int distance = len_X + len_Y;
	if (distance <= pow((m+7.5),2))
		return true;
	return false;*/
	return (pow(Pnt[pnt].x, 2) + pow(Pnt[pnt].y, 2)) <= pow((m + 7.5), 2) ? true : false;
}

bool DFS(int pnt) { /*深度搜索*/
	visited[pnt] = true;
	if (isSave(pnt)) {
        answer = true;
        return answer;
    }
	for (int i = 0; i < n; i++) {
		if (!visited[i] && jump(pnt, i)) { /*没访问过且在跳跃能力范围内*/
			answer = DFS(i);
			if (answer == true)
				break;
		}
	}
	return answer;
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> Pnt[i].x >> Pnt[i].y;
	}
	if (m > MINLEN) {
		cout << "Yes" << endl;
		return 0;
	}

	for (int i = 0; i < n; i++) {
		if (firstJump(i) && !visited[i]) {
			if (DFS(i))
				break;
		}
	}

	if (answer == true)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
	return 0;
}

测试结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值