C语言 06-图2 Saving James Bond - Easy Version

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37407587/article/details/80072651

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

Show me the Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MaxSize 100
struct Node
{
    int X;
    int Y;
};
typedef struct Node PtrToNode;
float STEP = 0;
int N = 0;
int Visited[MaxSize];
int Visit(PtrToNode V);
int DFS(int v, PtrToNode *G);
double distance(PtrToNode V,PtrToNode W);
int FirstJump(PtrToNode V);
void save007(PtrToNode *crocodile,int N);

int main()
{
    int i;
    for (i = 0;i < MaxSize; i++)
        Visited[i] = 0;

    scanf("%d %f", &N,&STEP);
    if (STEP >= 43)
    {
        printf("Yes\n");
        return 0;
    }
    PtrToNode crocodile[N];
    for(i = 0;i < N;i++)
    {
        scanf("%d %d",&crocodile[i].X, &crocodile[i].Y);
    }
    save007(crocodile,N);
    return 0;
}
void save007(PtrToNode *crocodile,int N)
{
    int i,answer = 0;
    for (i = 0;i < N;i++)
    {
        if(!Visited[i] && FirstJump(crocodile[i]))
        {
            Visited[i] = 1;
            answer = DFS(i,crocodile);
            if (answer == 1)
                break;
        }
    }
    if (answer == 1)
        printf("Yes\n");
    else printf("No\n");
}

int FirstJump(PtrToNode V)
{
    PtrToNode center;
    center.X = 0;
    center.Y = 0;
    int a = distance(center,V);
    if(distance(center,V) < STEP+7.5){
        return 1;
    }
    else
        return 0;
}
double distance(PtrToNode V,PtrToNode W)
{
    return sqrt(pow(V.X - W.X,2)+pow(V.Y-W.Y,2));
}
int DFS(int v, PtrToNode *G)
{
    int i, answer = 0;
    Visited[v] = 1;
    answer = Visit(G[v]);
    if(answer == 0)
    {
        for(i = 0; i < N; i++ )
        {
            if(!Visited[i] && distance(G[v],G[i]) <= STEP)
                answer = DFS(i,G);
            if (answer == 1)
                break;
        }
    }
    return answer;
}

int Visit(PtrToNode V)
{
    if (V.X <= STEP-50 || V.Y <= STEP-50 || V.X >= 50-STEP || V.Y >= 50-STEP)
        return 1;
    else
        return 0;
}

结果:

这里写图片描述

思路与问题:

本题虽然打着图的标签,但是事实上不需要建图就可以做出来,用的依然是深度优先搜索的思想,通过递归去实现对下一跳可行解的查找。注意的问题是,在题中,第一跳和其他跳有所不同,岸边也是图的节点之一。
在DFS中,递归逻辑要清晰,自己在实现的过程中出现了扫描的过程中误加了(未访问)的条件,导致一个不太显眼的bug,最终放到了IDE上进行调试。

猜你喜欢

转载自blog.csdn.net/m0_37407587/article/details/80072651