06-图2 Saving James Bond - Easy Version

题目:

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

算法逻辑与分析:
通过对题目的阅读,可以把该问题抽象为一个图的基于ListComponents的DFS遍历,即对图的连通分量的深度优先遍历,并在满足特定条件时就退出。

1.图的表达:
顶点:鳄鱼位置的结构数组,每个鳄鱼有一个编号V,对应坐标V(x,y)
边:边不是固定的,所以不生成图
权重:恒定为D
2.流程:
生成一个图,图只有顶点,没有边。【BuildLake】;
对图做ListComponents,选择一个顶点,满足【FirstJump】,并没有被访问过,用Visited数组跟踪顶点访问情况;
尝试跳跃到其它顶点【CanJumpOnto】,并判断是否到边【CanReachBank】;
到边即退出,否则对它的邻接点做DFS,直至结束;
释放申请的空间【FreeLake】。

手绘示意图:
06-图2 Saving James Bond - Easy Version_第1张图片

代码:

#include 
#include 
#include 
#include 

#define MaxNumOfCrocodile 100
#define DistanceToBank 50
#define IslandRadius 7.5

typedef int Vertex;

// 顶点(鳄鱼)的坐标
typedef struct _Crocodile
{
    int x, y;
} Crocodile[MaxNumOfCrocodile];

// Nv:鳄鱼的个数;D:跳跃距离;*Loc:鳄鱼的坐标
struct _Lake
{
    int Nv, D;
    Crocodile Loc;
};
typedef struct _Lake *Lake;

Lake BuildLake();
bool FirstJump(Lake L, Vertex V);
bool CanJumpOnto(Lake L, Vertex V, Vertex W);
bool CanReachBank(Lake L, Vertex V);
bool DFS(Lake L, Vertex V, bool Visited[]);
void Save007(Lake L);
void FreeLake(Lake L);

/*
06-图2 Saving James Bond - Easy Version
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

yes

4 13
-12 12
12 12
-12 -12
12 -12

no

 */

int main()
{
    Lake L = BuildLake();
    Save007(L);
    FreeLake(L);

    return 0;
}

Lake BuildLake()
{
    Lake L = (Lake)malloc(sizeof(struct _Lake));
    scanf("%d %d", &L->Nv, &L->D);
    Vertex V;
    for (V = 0; V < L->Nv; V++)
        scanf("%d %d", &L->Loc[V].x, &L->Loc[V].y);

    return L;
}

bool FirstJump(Lake L, Vertex V)
{
    float distance = sqrt(pow(L->Loc[V].x, 2) + pow(L->Loc[V].y, 2));
    if (distance - IslandRadius <= L->D)
        return true;
    else
        return false;
}

bool CanJumpOnto(Lake L, Vertex V, Vertex W)
{
    float distance = sqrt(pow(L->Loc[V].x - L->Loc[W].x, 2) + pow(L->Loc[V].y - L->Loc[W].y, 2));
    if (distance <= L->D)
        return true;
    else
        return false;
}

bool CanReachBank(Lake L, Vertex V)
{
    if (DistanceToBank - abs(L->Loc[V].x) <= L->D || DistanceToBank - abs(L->Loc[V].y) <= L->D)
        return true;
    else
        return false;
}

bool DFS(Lake L, Vertex V, bool Visited[])
{
    Visited[V] = true;
    bool isSafety = false;
    if (CanReachBank(L, V))
    {
        isSafety = true;
    }
    else
    {
        Vertex W;
        for (W = 0; W < L->Nv; W++)
        {
            if (!Visited[W] && CanJumpOnto(L, V, W))
                isSafety = DFS(L, W, Visited);
            if (isSafety)
                break;
        }
    }

    return isSafety;
}


// 核心算法
void Save007(Lake L)
{
    // 初始化顶点访问数组
    bool Visited[L->Nv];
    Vertex V;
    for (V = 0; V < L->Nv; V++)
    {
        Visited[V] = false;
    }

    // 根据该变量的值确定解救是否成功
    bool isSafety = false;

    // 遍历每个可以第一跳的顶点(鳄鱼),再根据该顶点DFS寻找出口,并返回结果
    for (V = 0; V < L->Nv; V++)
    {
        if (!Visited[V] && FirstJump(L, V))
        {
            isSafety = DFS(L, V, Visited);
            if (isSafety)
            {
                break;
            }
        }
    }

    if (isSafety)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }
}


void FreeLake(Lake L)
{
    free(L);
}

测试结果:
06-图2 Saving James Bond - Easy Version_第2张图片

你可能感兴趣的:(算法,深度优先)