06-图2 Saving James Bond

题目描述

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

解题思路

典型的DFS问题,用一个一维数组存鳄鱼坐标,这相当于图的顶点,再用一个数组判断是否已经访问。和常规DFS问题一样,DFS时需要判断两个条件:是否有边,是否访问过。是否有边在这里相当于能否跳跃,因此可以写一个函数来实现,值得注意的是,第一次跳跃和其它跳跃不同,因此需要写两个类似的函数;是否访问过就用之前那个数组判断即可。另外,当能够逃脱的时候需要提前退出DFS。

代码

#include <stdio.h>
#include <math.h>

#define MAXSIZE 100

//鳄鱼的坐标
typedef struct {
    int x;
    int y;
} Coordinate;

int visited[MAXSIZE] = {0};

void listConnectedSet_DFS(Coordinate crocodiles[], int N, int D);
int DFS(Coordinate crocodiles[], int N, int D, Coordinate crocodile);
int canFirstJump(Coordinate crocodile, int D);
int canJump(Coordinate crocodile, int x, int y, int D);

int main() {
    int N, D;
    Coordinate crocodiles[MAXSIZE];
    scanf("%d %d", &N, &D);
    for (int i = 0; i < N; i++) {
        scanf("%d %d", &crocodiles[i].x, &crocodiles[i].y);
    }
    listConnectedSet_DFS(crocodiles, N, D);
    return 0;
}

//对整个图DFS,若能够逃脱则打印Yes,否则打印No
void listConnectedSet_DFS(Coordinate crocodiles[], int N, int D) {
    int ret = 0;
    for (int i = 0; i < N; i++) {
        if (!visited[i] && canFirstJump(crocodiles[i], D)) {
            visited[i] = 1;
            ret = DFS(crocodiles, N, D, crocodiles[i]);
            if (ret == 1) break;
        }
    }
    if (ret == 1) printf("Yes
");
    else printf("No
");
}

//对每一个连通集DFS,若能够逃脱则返回1,否则继续递归
int DFS(Coordinate crocodiles[], int N, int D, Coordinate crocodile) {
    int ret = 0;
    int x = crocodile.x, y = crocodile.y;
    if (abs(x) + D >= 50 || abs(y) + D >= 50) return 1;     //提前退出
    for (int i = 0; i < N; i++) {
        if (!visited[i] && canJump(crocodiles[i], x, y, D)) {
            visited[i] = 1;
            ret = DFS(crocodiles, N, D, crocodiles[i]);
            if (ret == 1) return 1;
        }
    }
    return 0;
}

//判断第一次能否跳跃
int canFirstJump(Coordinate crocodile, int D) {
    double canJump = D + 7.5;
    int x = crocodile.x;
    int y = crocodile.y;
    if (pow(x, 2) + pow(y, 2) <= pow(canJump, 2))
        return 1;
    else
        return 0;
}

//在鳄鱼头上时判断能否跳跃
int canJump(Coordinate crocodile, int x, int y, int D) {
    int x1 = crocodile.x;
    int y1 = crocodile.y;
    if (pow(x - x1, 2) + pow(y - y1, 2) <= pow(D, 2))
        return 1;
    else
        return 0;
}
原文地址:https://www.cnblogs.com/AndyHY-Notes/p/12581724.html