【从0开始的刷题生活】PTA数据结构2019-秋 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.

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

题目简述

007要从一个100X100的正方形小河的中心位置开始跳跃,小河中心处有一个直径为15的圆盘,在小河的其他地方有数量为N的鳄鱼,007每次最远能跳D远,问007能否跳到安全位置。
输入第一行的为N和D,N为鳄鱼个数,D为007能跳的最远距离,其下的N行为每个鳄鱼的x和y坐标
若007能跳到安全位置输出Yes,否则输出No。

思路分析

根据给出的输入我们可以考虑用一个邻接矩阵模拟一个图,之后使用dfs遍历每一个可能点,在浙江大学的数据结构MOOC中,陈越老师给出了一个dfs思路,在下面的代码中函数 Save007()DFS() 中有清楚的体现。
为了方便条件判断,使用了math函数库中的 abs() 函数,通过某个坐标的绝对值来判断。

代码

#include <iostream>
#include <cmath>
#define maxn 105
using namespace std;
struct pos{
	int x,y;
}a[maxn];
bool visited[maxn];
int n,d,answer;
int FirstJump(int i)//判断007能否起跳
{
	if(abs(a[i].x)<=8+d && abs(a[i].y)<=8+d) return 1;
	else return 0;
}
int IsSafe(int i)//判断007是否安全
{
	if(abs(a[i].x)+d>=50 || abs(a[i].y)+d>=50) return 1;
	else return 0;
}
int Jump(int i,int j)//判断007能否跳到下一个鳄鱼处
{
	if(abs(a[j].x)-abs(a[i].x)<=d && abs(a[j].y)-abs(a[i].y)<=d)
		return 1;
	else return 0;
}
int DFS(int V)//dfs遍历每一个点
{
	visited[V]=true;
	if(IsSafe(V)) answer=1;
	else{
		for(int W=0;W<n;W++)
			if(!visited[W] && Jump(V,W)){
				answer=DFS(W);
				if(answer==1) break;
			}
	}
	return answer;
}
void Save007(struct pos G[])//功能函数
{
	for(int V=0;V<n;V++)
		if(!visited[V] && FirstJump(V)){//初始起跳判断
			answer=DFS(V);
			if(answer==1) break;
		}
	if(answer==1) cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
}
int main()
{
	cin>>n>>d;
	for(int i=0;i<n;i++)
		visited[i]=false;
	for(int i=0;i<n;i++)
		cin>>a[i].x>>a[i].y;
	Save007(a);	
}

在这里插入图片描述

写在后面

本题的数据较弱,第一次提交时只有测试点3未过,检查代码发现是函数 int Jump() 的判断条件写错,改了之后提交AC,但重新阅读题目发现我把直径看成了半径…又把函数 int FirstJump() 的条件改成了现在代码中的

if(abs(a[i].x)<=8+d && abs(a[i].y)<=8+d)

与原先的代码中的判断条件

if(abs(a[i].x)<=15+d && abs(a[i].y)<=15+d)

一样都可以AC
呃呃呃…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值