1. 程式人生 > >凜冬之翼---拯救007

凜冬之翼---拯救007

花了一個上午寫出來的程式碼,這道題目的背景很有意思,我小時候也看過007的電影中007從鱷魚池逃跑的片段感覺題目很有意思。今天早點睡覺,明天早一點到實驗室。

題目:
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)。 中央島是直徑為15,以(0,0)為中心的圓盤。許多鱷魚在湖的不同位置。考慮到每個鱷魚的座標和詹姆斯可以跳躍的距離,你必須告訴他他是否可以逃脫。

Input Specification:
Each input file contains one test case. Each case starts with a line containing two positive integers NN (\le 100≤100), the number of crocodiles, and DD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.

每個輸入檔案包含一個測試用例。 每個案例從一個包含兩個正整數的行開始,鱷魚的數量N(≤100)和詹姆斯可以跳躍的最大距離D。 接下來的N行,每個包含鱷魚的(x,y)位置。 注意,沒有兩個鱷魚停留在相同的位置。

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

對於每個測試用例,如果James可以逃脫,則在行中輸入“是”,否則輸出“否”。

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

解題思路:
1.這裡我主要使用了DFS的思路來解題,其中有遞迴的思想,就是當007跳到一個鱷魚頭上時,就把這個鱷魚頭標記為已經訪問過,然後再遞迴訪問周圍所有可以訪問的鱷魚頭,遞迴到下一個鱷魚頭之前先檢查是否可以從這個鱷魚頭跳上岸。
2.資料結構用一個數軸crocodile[Max]來表示,它的結構裡面包含了一個座標的x值和y值。然後用另一個visited數軸表示是否訪問過。

遇到的問題:
1.其中false和true是用字元還是整形來表示有點頭暈,直接在標頭檔案下面定義#define false 0 #define true 1 來得實際。
2.在呼叫函式時後面的()錯誤打成了[]花了好多時間找出來,下次要仔細。
3.多些一點註釋,因為現在的演算法也開始逐漸複雜起來了,不寫註解一段時間再看就有點摸不著頭腦了。以上。

程式碼:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define Max 101
#define True 1
#define False 0

struct crocodile{       //the struct define the x and y coordinate of the crocodile
	int x;
	int y;
}crocodile[Max];

int result=False;
int visited[Max]={0};  //using visited to pretend whether the crocodile has been visited 
int N,D;        //maro define the Max number of crocodile and the longest distance

double Distance(int i,int j){    //calculate distance between two crocodiles
	double x;
	x=sqrt(pow(crocodile[i].x-crocodile[j].x,2)+pow(crocodile[i].y-crocodile[j].y,2)); //sqrt (xi-yj)^2-(yi-yj)^2
	return x;
}

int Escape(int i){             //the check whether 007 can jump from crocodile to bank
    if((crocodile[i].x<=D-50)||(crocodile[i].x>=50-D)||(crocodile[i].y<=D-50)||(crocodile[i].y>=50-D))
	return True;
	else
	return False; 
}

int DFS(int i){
	visited[i]=True;
	if(Escape(i)) result=True;    //if the crocodile can escape set yes
	else{
		for(int j=1;j<=N;j++){
		if((!visited[j])&&(Distance(i,j)<=D)){  //if the crocodile is'nt visited and 007 can jump to it 
			result=DFS(j);
		}
		}		
	}
	return result;  	
}



int main(){
    scanf("%d %d",&N,&D);
    visited[0]=True;
    crocodile[0].x=0;
    crocodile[0].y=0;    //first begin in the middle of the (0,0)
    for(int i=1;i<=N;i++){  //get the coordinate of the crocodile
    	scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
	}
    for(int i=1;i<=N;i++){       //from i=1 to N to jump 
    if(Distance(0,i)<=(D+7.5)) {  //whether 007 can jump from (0,0) to one crocodile
    		result=DFS(i);
    		if(result){
    			printf("Yes"); break;
			}
	}	
	}
	
	if(!result) printf("No");
    
}