Scott's world.

Saving James Bond - Easy Version

Word count: 662Reading time: 3 min
2019/08/14 Share

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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:

1
Yes

Sample Input 2:

1
2
3
4
5
4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

1
No

题目分析

  • 这道题显而易见是需要采用图中的深度遍历DFS来得到结果,其中图结构在这道题中最适合的是邻接矩阵来表示

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <queue>
#include <math.h>

const double LAND_RADIUS=15.0/2;
const double LAKE_SIZE=100.0;
const int N=110;

typedef struct{
double x,y;
}Position;

Position P[N];
bool Visited[N];
int n;
double d;

void Save007();
bool DFS(int V);
bool Jump(int V1,int V2);
bool FirstJump(int V);
bool IsSave(int V);

int main()
{
scanf("%d%lf",&n,&d);
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&P[i].x,&P[i].y);
}
for(int i=0;i<n;i++)
Visited[i]=false;
Save007();
}


bool DFS(int V)
{
Visited[V]=true;
bool answer=false;
if(IsSave(V))
return true;
for(int i=0;i<n;i++)
{
if(!Visited[i]&&Jump(V,i))
answer=DFS(i);
if(answer)
break;
}
return answer;
}

void Save007()
{
bool answer=false;
for(int i=0;i<n;i++)
{
if(!Visited[i]&&FirstJump(i)){
answer=DFS(i);
if(answer)
break;
}
}
if (answer){
printf("Yes\n");
}else{
printf("No\n");
}
}

bool IsSave(int V)
{
return (abs(P[V].x)>=50-d||
abs(P[V].y)>=50-d);
}

bool FirstJump(int V)
{
return sqrt(P[V].x*P[V].x+P[V].y*P[V].y)<=d+LAND_RADIUS;
}

bool Jump(int V1,int V2)
{
return sqrt((P[V1].x-P[V2].x)*(P[V1].x-P[V2].x)+
(P[V1].y-P[V2].y)*(P[V1].y-P[V2].y))<=d;
}
CATALOG
  1. 1. Saving James Bond - Easy Version
    1. 1.0.1. Input Specification:
    2. 1.0.2. Output Specification:
    3. 1.0.3. Sample Input 1:
    4. 1.0.4. Sample Output 1:
    5. 1.0.5. Sample Input 2:
    6. 1.0.6. Sample Output 2:
  2. 1.1. 题目分析
  3. 1.2. 代码实现