PTA_16_05_图2 _Saving _James _Bond _Easy _Version

PTA_16_05_图2 _Saving _James _Bond _Easy _Version

题目分析:

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.

这一次让我们考虑电影中的“活生生和让人死亡”的情形,詹姆斯·邦德是世界上最著名的间谍,他被一群毒品贩子抓住了。他被送到湖心的一小块土地上,那里到处都是鳄鱼。在那里,他做了最大胆的逃跑动作——他跳到了最近一条鳄鱼的头上!在动物意识到发生了什么之前,詹姆斯又跳到了下一个大脑袋上。。。最后,在最后一条鳄鱼咬他之前,他终于到达了河岸(事实上,特技演员被大嘴抓住了,只穿了一双厚厚的靴子就勉强逃脱了)。

假设这个湖是一个100乘100平方米的湖。假设湖的中心位于(0,0),东北角位于(50,50)。中心岛是一个以(0,0)为中心、直径为15的圆盘。许多鳄鱼在湖中的不同位置。根据每只鳄鱼的坐标和詹姆斯能跳的距离,你必须告诉他他是否能逃脱。

输入数据样式

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.

每个输入文件包含一个测试用例。每种情况都从一行开始,该行包含两个正整数N(≤100),鳄鱼的数量和D,詹姆斯能跳的最大距离。接着是N行,每行包含一条鳄鱼的(xy)位置。请注意,没有两只鳄鱼停留在同一位置。

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

    
    
    
*************
    
4 13
-12 12
12 12
-12 -12
12 -12

输出数据格式

For each test case, print in a line “Yes” if James can escape, or “No” if not.

对于每一个测试用例,输出yes或则no

yes
*******
no

问题分析:

拯救james这道题,最好的方法就是画个图,根据图进行分析。

首先james第一跳很重要,需要考虑小岛的半径,然后能否跳到

所以应该是对所有的鳄鱼进行判断。

本题其实应该是上一题的,深度遍历的推广,对所有的鳄鱼要进行标记,也是利用递归的思想进行求解的,代码逻辑比较简单

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;

#define MAXSIZE 100


struct EyuNode
{
	int x;
	int y;
}eyu[MAXSIZE];

bool visited[MAXSIZE];

void CreateEyu(int num)
{
	int i, j;
	for (i = 0; i < num; i++)//输入的鳄鱼坐标是有关原点相对的坐标
	{
		cin >> eyu[i].x >> eyu[i].y;
	}
}


int firstjump(int i,int jump)//直接将鳄鱼的坐标与源点的绝对距离进行判断
{
	int t;
	t = pow(eyu[i].x, 2) + pow(eyu[i].y, 2);
	t = sqrt(t);
	if (t <= jump+15)//得加上小岛的半径,小岛是半径为15的圆
		return 1;
	else
		return 0;
}


int IsSafe(int i,int jump)//因为河岸边的距离都为50,50,只需计算这条鳄鱼的x,y与河岸边的距离小于跳跃长度就可以
{
	int x_distance, y_distance;
	x_distance = abs(eyu[i].x - 50);
	y_distance = abs(eyu[i].y - 50);
	if (x_distance <= jump || y_distance <= jump)
	{
		return 1;
	}
	else
		return 0;
}

int JumpAble(int i, int w, int jump)
{
	int t;
	t = pow(eyu[i].x-eyu[w].x, 2) + pow(eyu[i].y-eyu[w].y, 2);
	t = sqrt(t);
	if (t <= jump )//得加上小岛的半径,小岛是半径为15的圆
		return 1;
	else
		return 0;
}



int DFS(int i,int num,int jump)
{
	int w;
	int answer = 0;
	visited[i] = true;
	//首先判断这条鳄鱼能不能直接跳到岸上
	if (IsSafe(i, jump))
		return 1;
	else
	{
		for (w = 0; w < num; w++)//遍历所有的其余的鳄鱼
		{
			if (!visited[w] && JumpAble(i, w, jump))//如果这条鳄鱼没被访问过,并且能够跳的上,则进行下一次的深度遍历
			{
				answer=DFS(w,num,jump);

				if (answer == 1)
				{
					break;
				}
			}
		}
	}
	return answer;

}


void save007(int num,int jump)
{
	int i;
	int answer=0;
	for (i = 0; i < num; i++)
	{
		visited[i] = false;//初始化标记数组
	}


	for (i = 0; i < num; i++)//遍历所有的鳄鱼,对每条鳄鱼路径进行判断
	{
		if (!visited[i] && firstjump(i, jump))//首先判断鳄鱼是否被访问过,同时判断,能否可以跳到鳄鱼的身上
		{
			answer = DFS(i,num,jump);
		}
	}
	if (answer)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;
	}
}



int main()
{
	int num, jump;
	cin >> num >> jump;
	CreateEyu(num);
	save007(num,jump);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值