7-10 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 (≤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
這道題考察的主要是圖的遍歷-DFS的應用,需要注意的是Bond的第一跳與之後的其他跳不同,主要為所形成的圓的半徑要加上湖中心島的半徑;其次,Bond的第一跳可能有多種選擇,這些選擇都有可能會產生逃生路徑,所以使用DFS進行遍歷的時候要遍歷圖中所有的連通集。具體程式碼實現為:
#include<stdio.h>
#include<math.h>
#define YES 1
#define NO 0
struct coordinate{ //鱷魚座標
int x;
int y;
}crocodile[105];
int N,D;
int visited[105] = {0}; //標誌某一個鱷魚座標是否被訪問過,=0未被訪問過
void Save007();
int main()
{
int i;
scanf("%d %d",&N,&D); //輸入鱷魚數量及007可以跳躍的最大距離
for(i=0; i<N; i++){
scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
}
Save007();
return 0;
}
int IsSafe(int v)
{
int X_distant,Y_distant;
X_distant = abs(crocodile[v].x)-50;
Y_distant = abs(crocodile[v].y)-50;
return (abs(X_distant) <= D || abs(Y_distant) <= D);
}
int Jump(int v,int w)
{
return (sqrt(pow(crocodile[v].x-crocodile[w].x,2)+pow(crocodile[v].y-crocodile[w].y,2)) <= D); //兩個鱷魚座標之間的距離
}
int DFS(int v)
{
int w;
int answer = NO;
visited[v] = 1; //表明w被訪問過
if ( IsSafe(v) ) answer = YES;
else{
for(w=0; w<N; w++){
if( !visited[w] && Jump(v,w)){
answer = DFS(w);
if(answer == YES) break;
}
}
}
return answer;
}
int FirstJump(int v)
{
return (sqrt(pow(crocodile[v].x,2)+pow(crocodile[v].y,2)) <= (D+15));
}
void Save007()
{
int v;
int answer = NO;
for(v=0; v<N; v++){
if( !visited[v] && FirstJump(v) ){
answer = DFS(v);
if(answer == YES) break;
}
}
if(answer == YES) printf("Yes\n");
else printf("No\n");
}