Problem Description
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
Solution
题意理解:
给定鳄鱼坐标和跳跃半径,湖心岛直径和岸边坐标已知,求是否可以从湖心岛通过鳄鱼头上跳到岸上。
以下是很自然想到的一种中规中矩的遍历方法:
首先读入各鳄鱼坐标,以下标 0 为湖心岛,下标 N + 1 为岸边,下标 1 ~ N 为鳄鱼,建立结点数为 N + 2 的一个图,从 0 开始搜索,搜索完毕后检查 N + 1 是否被访问,如果被访问说明可以逃脱,否则不行。
具体实现:
用结构体数组来存每只鳄鱼的坐标,便于计算。
建图过程为:
- 先特判能否一步跳到岸上,如果能就把 0 与 N + 1 连接
- 然后是遍历 1 ~ N 这 N 个鳄鱼
- 如果能从湖心岛一部跳上,把 0 与 i 连接
- 如果能跳到岛上,把 i 与 N + 1 连接
- 然后遍历鳄鱼与鳄鱼直接能否跳跃,如果能就把 i 与 j 连接
进行 BFS 后如果 N + 1 被访问过就能够逃脱,否则不能。
注意:三个判断函数扩大倍数时不要弄错
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct CNode Crocodile;
struct CNode {
int x;
int y;
};
void Input(Crocodile C[], int *N, int *D)
{
int i, a, b;
scanf("%d%d", &(*N), &(*D));
for (i = 1; i <= *N; i++) {
scanf("%d%d", &a, &b);
C[i].x = a;
C[i].y = b;
}
}
bool FirstJump(Crocodile C[], int i, int D)
{
int distance2;
distance2 = C[i].x * C[i].x + C[i].y * C[i].y;
if (distance2 * 100 <= (D * 10 + 75) * (D * 10 + 75)) return true;
else return false;
}
bool IsSafe(Crocodile C[], int i, int D)
{
if (C[i].x + D >= 50 || C[i].x - D <= -50 || C[i].y + D >= 50 || C[i].y - D <= -50) return true;
else return false;
}
bool Jump(Crocodile C[], int i, int j, int D)
{
int distance2;
distance2 = (C[i].x - C[j].x) * (C[i].x - C[j].x) + (C[i].y - C[j].y) * (C[i].y - C[j].y);
if (distance2 <= D * D) return true;
else return false;
}
void BuildGraph(Crocodile C[], int G[][102], int N, int D)
{
int i, j;
if (75 + D * 10 >= 500) G[0][N + 1] = G[N + 1][0] = 1;
for (i = 1; i <= N; i++) {
if (FirstJump(C, i, D)) G[0][i] = G[i][0] = 1;
if (IsSafe(C, i, D)) G[i][N + 1] = G[N + 1][i] = 1;
for (j = 1; j <= N; j++) {
if (Jump(C, i, j, D)) G[i][j] = G[j][i] = 1;
}
}
}
int front, rear, queue[102];
void Search(int G[][102], int N)
{
int i, v, visited[102] = {0};
front = rear = 0;
queue[rear++] = 0;
visited[0] = 1;
while (front != rear) {
v = queue[front++];
for (i = 0; i <= N + 1; i++) {
if (!visited[i] && G[i][v] == 1) {
queue[rear++] = i;
visited[i] = 1;
}
}
}
if (visited[N + 1]) printf("Yes\n");
else printf("No\n");
}
int main()
{
int i, j, N, D;
int G[102][102] = {0};
Crocodile C[102];
Input(C, &N, &D);
BuildGraph(C, G, N, D);
Search(G, N);
return 0;
}