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.
#include<iostream>
#include<cmath>
using namespace std;
#define MaxNum 101
struct Node {
int x, y;
bool flag;
};
typedef struct Node Position;
Position cro[MaxNum];
bool answer = false;
void Save007(int N, int D);
int main()
{
int N, D, i;
cin >> N >> D;
for (i = 0; i < N; ++i) {
cin >> cro[i].x >> cro[i].y;
cro[i].flag = false;
}
Save007(N, D);
return 0;
}
//计算是否能从v1 跳到 v2
bool Calculate(Position v1, Position v2, int D)
{
double distance;
distance = sqrt(pow((v1.x - v2.x), 2) + pow((v1.y - v2.y), 2));
if (D >= distance) return true;
else return false;
}
//第一次跳跃是否成功
bool FirstJump(int V, int D)
{
Position Point; //原点
Point.x = 0;
Point.y = 0;
Point.flag = true;
if (Calculate(cro[V], Point, D+7.5)) { //传入岛的半径+跳跃距离
return true;
}
else {
return false;
}
}
//是否已经到岸
bool IsSafe(int V, int D)
{
int distance1 = abs(cro[V].x) + D;
int distance2 = abs(cro[V].y) + D;
if (distance1 >= 50 || distance2 >= 50) {
return true;
}
return false;
}
//深度优先搜索
void DFS(int V, int D, int N)
{
int W;
cro[V].flag = true; //跳到V将flag改为true
if (IsSafe(V, D)) { //如果上岸
answer = true;
return;
}
else {
for (W = 0; W < N; ++W) { //遍历所有节点
if (!cro[W].flag && Calculate(cro[V], cro[W], D)) { //未被访问过且在跳跃距离内
DFS(W, D, N); //跳入附近节点
if (answer) break;
}
}
}
}
void Save007(int N, int D)
{
int V;
for (V = 0; V < N; ++V) {
if (!cro[V].flag && FirstJump(V, D)) { //未被访问过且可以从岛上跳到
DFS(V, D, N);
if (answer) break;
}
}
if (answer) cout << "Yes" << endl;
else cout << "No" << endl;
}