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, andD, 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
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
const int YES = 1;
struct pos{
int x;
int y;
};
pos *Crocodile;
void Save007(void); //扫描所有顶点,如果能跳过去,则调用深度优先搜索
int Dfs(int V); //深度优先搜索
bool FirstJump(int V); //第一跳判断
bool Jump(int V, int W); //两只鳄鱼之间的距离判断
bool IsSafe(int V); //到达某个鳄鱼头后,判断是否能跳到岸上
int Cal(int a, int b); //计算两个坐标之间的直角边距离
int N,Dis;
int visited[101] = {0}; //顶点访问记录
int main()
{
int i,x,y;
cin >> N >> Dis;
Crocodile = new pos[N+1];
Crocodile[0].x = 0;
Crocodile[0].y = 0;
for(i = 1; i <= N; i++)
{
cin >> x >> y;
Crocodile[i].x = x;
Crocodile[i].y = y;
}
Save007();
return 0;
}
void Save007()
{
int i,ans;
for(i = 0; i <= N ;i++){
if (!visited[i] && FirstJump(i)){
ans = Dfs(i);
if (ans == YES) break;
}
}
if (ans == YES) cout << "Yes" << endl;
else cout << "No" << endl;
}
int Dfs(int V)
{
visited[V] = 1; //该顶点被访问
int i,ans = 0;
if (IsSafe(V)) ans = YES;
else
for ( i = 0; i <= N; i++)
{
if ((!visited[i])&&(Jump(V , i))){
ans = Dfs(i);
if (ans == YES) break;
}
}
return ans;
}
bool IsSafe(int V)
{
int x,y;
x = abs(Crocodile[V].x);
y = abs(Crocodile[V].y);
if (x + Dis >= 50 || y + Dis >= 50)
return true;
else
return false;
}
bool FirstJump(int V)
{
int x,y;
double dis;
x = abs(Crocodile[V].x);
y = abs(Crocodile[V].y);
dis = sqrt(x * x + y * y);
if (dis <= Dis + 7.5) return true; //小岛半径7.5米
else return false;
}
bool Jump(int V, int W)
{
int x = Cal(Crocodile[V].x , Crocodile[W].x);
int y = Cal(Crocodile[V].y , Crocodile[W].y);
double dis = sqrt( x * x + y * y);
if (dis <= Dis) return true; //教训,名字取的一样,意思搞反了,建立变量名还是要小心
else return false;
}
int Cal(int a, int b)
{
if (a * b > 0) return abs(abs(a) - abs(b));
else return abs(a) + abs(b);
}