拯救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;
} 

  測驗結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM