专栏首页机器学习与自然语言处理06-图2 Saving James Bond - Easy Version

06-图2 Saving James Bond - Easy Version

题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625

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代表一种拯救方案,只要存在一种方案即可获救
*/
#include <cstdio>
#include <cmath>
#include <cstdlib>

const double ISLAND_RADIUS = 15.0 / 2;      //孤岛半径
const double SQUARE_SIZE = 100.0;           //湖(正方形)的大小
const int N = 110;                          //鳄鱼(点)的最大数

typedef struct Point
{
    double x, y;
} Position;

Position P[N];
bool Visited[N];
int n;
double d;

void Save007();
bool DFS(int V);
bool FirstJump(int V);
bool Jump(int V1, int V2);
bool IsSave(int V);

int main()
{
    scanf("%d%lf", &n, &d);
    for (int i = 0; i < n; i++)
        scanf("%lf%lf", &(P[i].x), &(P[i].y));

    for (int i = 0; i < n; i++)
        Visited[i] = false;
    Save007();

    return 0;
}

void Save007()
{
    bool IsSave = false;
    for (int i = 0; i < n; i++)
    {
        if (!Visited[i] && FirstJump(i))
        {
            IsSave = DFS(i);
            if (IsSave)
                break;
        }
    }
    if (IsSave)
        printf("Yes\n");
    else
        printf("No\n");
}

bool DFS(int V)
{
    Visited[V] = true;
    bool answer = false;
    if (IsSave(V))
        return true;
    for (int i = 0; i < n; i++)
    {
        if (!Visited[i] && Jump(V, i))
            answer = DFS(i);
        if (answer)
            break;
    }
    return answer;
}

bool IsSave(int V)
{
    return (abs(P[V].x) >= 50 - d)
            || (abs(P[V].y) >= 50 - d);
}

bool FirstJump(int V)
{
    return sqrt(P[V].x * P[V].x + P[V].y * P[V].y)
            <= d + ISLAND_RADIUS;
}

bool Jump(int V1, int V2)
{
    return sqrt((P[V1].x - P[V2].x) * (P[V1].x - P[V2].x) +
                (P[V1].y - P[V2].y) * (P[V1].y - P[V2].y))
            <= d;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 04-树6. Huffman Codes--优先队列(堆)在哈夫曼树与哈夫曼编码上的应用

    题目来源:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%916 In 1953, David A. Huffm...

    llhthinker
  • POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS ...

    llhthinker
  • 03-树2. List Leaves (25) 二叉树的层序遍历

    03-树2. List Leaves (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912...

    llhthinker
  • HDUOJ---2642Stars(二维树状数组)

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Jav...

    Gxjun
  • 浙江大学计算机与软件学院2019年保研上机模拟练习

    A happy number is defined by the following process: Starting with any positive i...

    glm233
  • LeetCode 477 Total Hamming Distance

    The Hamming distance between two integers is the number of positions at which th...

    Yano_nankai
  • poj-1207 THE 3n+1 problem

    Problems in Computer Science are often classified as belonging to a certain clas...

    瑾诺学长
  • poj 3126 Prime Path

    Description ? The ministers of the cabinet were quite upset by the message from ...

    用户1624346
  • 终极一战:为了编程面试!

    过去常常读一个问题,然后花几分钟把它映射到我以前见过的类似问题上。如果我可以映射它,我将只关注这个问题与父问题相比有哪些不同约束。如果这是一个新问题,那么我会尝...

    量化投资与机器学习微信公众号
  • Code Forces 645A Amity Assessment

    A. Amity Assessment time limit per test2 seconds memory limit per test256 me...

    ShenduCC

扫码关注云+社区

领取腾讯云代金券