拯救007

题目说明:

 

在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据说当年替身演员被最后一条鳄鱼咬住了脚,幸好穿的是特别加厚的靴子才逃过一劫。)

 

设鳄鱼池是长宽为100米的方形,中心坐标为 (0, 0),且东北角坐标为 (50, 50)。池心岛是以 (0, 0) 为圆心、直径15米的圆。给定池中分布的鳄鱼的坐标、以及007一次能跳跃的最大距离,你需要告诉他是否有可能逃出生天。

 

 

 

输入格式:

 

首先第一行给出两个正整数:鳄鱼数量 N(≤)和007一次能跳跃的最大距离 D。随后 N 行,每行给出一条鳄鱼的 ( 坐标。注意:不会有两条鳄鱼待在同一个点上)。

 

输出格式:

 

如果007有可能逃脱,就在一行中输出"Yes",否则输出"No"。

 

 

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.

 

Input Specification:

Each input file contains one test case. Each case starts with a line containing two positive integers N (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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 1:

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

  

Sample Output 1:

No

  

 题目分析:

这道题目是学浙大数据结构做的,根据老师所给的总体算法,我们要写出主体函数Save007,Save007应包括FirstJump,DFS,IsSafe,Jump

判断要点:

    ①湖是一个正方形,边长为100,中心在(0,0)四个定点分别为(50,50),(50,-50),(-50,-50),(-50,50),湖中小岛直径是15,半径7.5.如果007步长d大于50-7.5=42.5,就可以直接从小岛直接跳到湖岸

    ②判断007能否从岛上跳到湖中某一点A(x, y),即d+7.5>=sqrt(x*x+y*y);也就是(d+7.5)*(d+7.5)>=x*x+y*y;(FirstJump函数)

    ③判断007能否从A点直接跳到湖岸,当007步长大于A点到湖岸的距离时,说明可以到达换,即d>= 50-| x | 或者d>= 50-| y |;(IsSafe函数)

    ④如果跳上一点不能直接跳到湖岸,那就跳上另一点看能不能跳到湖岸满足③条件,这里就是判断能否从A(x1,y1)点跳到B(x2,y2)点,如果007的步长d>=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)),即d*d >= (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)。如果满足,说明可以从A点跳到B点,否则不行。(Jump+DFS函数)如果不行,就继续递归尝试另一个B点,如果此路不通,返回②条件,换一条路,换一个起点A继续尝试,如果所有路都无法到达对岸,说明无法逃生。

代码如下:

#include<iostream>
using namespace std;
#define MAX 100
#define YES	1
#define NO 0 
int d;//步长 
int Visited[MAX];//标记矩阵 
/*初始化标记矩阵*/ 
void ResetVisit(){
	for(int i=0;i<MAX;i++)
		Visited[i]=0;
}
int N;
//存储鳄鱼方位 
struct Point{
	int x;
	int y;	
}Point[MAX];

/*第一跳*/
int FirstJump(int i){
	int x=Point[i].x;
	int y=Point[i].y;
	if((d+7.5)*(d+7.5)>=(x*x+y*y))
		return YES;
	else return NO;
}
/*是否能从i点跳跃至j点*/
int Jump(int i,int j){
	int x1=Point[i].x;
	int y1=Point[i].y;
	int x2=Point[j].x;
	int y2=Point[j].y;
	if(d*d>=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
		return YES;
	else return NO;
}
/*在i点判断是否能到达岸边*/ 
int IsSafe(int i){
	int x=Point[i].x;
	int y=Point[i].y;
	if(x<0)
		x=-x;
	if(y<0)
		y=-y;
	if(d>=50-x||d>=50-y)
		return YES;
	else return NO;
}
/*dfs*/
int DFS(int i){
	int answer;
	Visited[i]=true;
	if(IsSafe(i)) answer=YES;
	else{
		//循环遍历每个节点 
		for(int j=0;j<N;j++){
			//如果该节点未被访问,且能从j结点跳至j结点 
			if(!Visited[j]&&Jump(i,j)){
				answer=DFS(j);
				if(answer==YES)  break;
			}
		}
	}
	return answer;
}
/*解救007*/
void Save007(){
	int answer;
	ResetVisit();//初始化标记矩阵 
	for(int i=0;i<N;i++){
		if(!Visited[i]&&FirstJump(i))
			answer=DFS(i);
			if(answer==YES) break;
	}
	if(answer==YES)	
		cout<<"Yes";
	else
		cout<<"No";
} 
int main(){
	cin>>N;
	cin>>d;
	for(int i=0;i<N;i++){
		cin>>Point[i].x;
		cin>>Point[i].y;
	}
	Save007();
	return 0;
} 

  测验结果:

posted @ 2018-05-16 22:37  Do_Better  阅读(3283)  评论(0编辑  收藏  举报