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
解题思路
题目大意:James Bond位于湖中央(0,0)处直径为15的小岛上,湖的长宽分别为100,即|x|=50,|y|=50为湖的边界,输入D为跳跃的距离,N为周围鳄鱼的个数,接下来N行输入鳄鱼的坐标,James Bond可不断踩着鳄鱼的头进行跳跃,判断是否能到达岸边。
在输入鳄鱼位置时,判断节点是否首跳,并判断是否能到达岸边,判断节点的可到达关系,创建图,采用DFS遍历判断是否能到达岸边。
#include<iostream>
#include<cmath>
using namespace std;
const int maxn = 100;
const double radius = 7.5; //半径
struct point{
int x,y;
bool first = false; //第一跳是否能到达
bool last = false; //是否能到岸边
}p[maxn];
int G[maxn][maxn] = {0};
bool vis[maxn] = {false};
int N,D;
bool flag = false; //能否达到岸边
bool reach(point p1, point p2){
if(sqrt(pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2)) <= D) //是否能到达
return true;
return false;
}
int Init(){
cin>>N>>D;
for(int i = 0; i < N; i++){
cin>>p[i].x>>p[i].y;
if(sqrt(pow(p[i].x,2)+pow(p[i].y,2)) <= radius + D) //首跳能到达
p[i].first = true;
if(abs(p[i].x + D) >= 50 || abs(p[i].y + D) >= 50) //最后一跳能到岸边
p[i].last = true;
}
for(int i = 0; i < N; i++){
for(int j = i + 1; j < N; j++){
if(reach(p[i],p[j]))
G[i][j] = G[j][i] = 1; //设置边可到达
}
}
}
void DFS(int v){
vis[v] = true; //已访问
if(p[v].last){
flag = true;
return;
}
for(int i = 0; i < N; i++){
if(!vis[i] && G[v][i]){
DFS(i);
}
}
}
int main()
{
Init();
for(int i = 0; i < N; i++){
if(!vis[i] && p[i].first)
DFS(i);
if(flag)
break;
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
}