1. 程式人生 > >C語言 06-圖2 Saving James Bond - Easy Version

C語言 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

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上進行除錯。