• PTA


    在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据说当年替身演员被最后一条鳄鱼咬住了脚,幸好穿的是特别加厚的靴子才逃过一劫。)
    设鳄鱼池是长宽为100米的方形,中心坐标为 (0, 0),且东北角坐标为 (50, 50)。池心岛是以 (0, 0) 为圆心、直径15米的圆。给定池中分布的鳄鱼的坐标、以及007一次能跳跃的最大距离,你需要告诉他是否有可能逃出生天。
    输入格式
    首先第一行给出两个正整数:鳄鱼数量 N(≤100)和007一次能跳跃的最大距离 D。随后 N 行,每行给出一条鳄鱼的 (x,y) 坐标。注意:不会有两条鳄鱼待在同一个点上。
    输出格式
    如果007有可能逃脱,就在一行中输出”Yes”,否则输出”No”。
    输入样例 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
    输出样例 1
    Yes
    输入样例 2
    4 13
    -12 12
    12 12
    -12 -12
    12 -12
    输出样例 2
    No

    思路

    一个边长100的鳄鱼池, 007在以(0,0)为中心, 15为直径的圆形小岛上, 需要踩着鳄鱼头往外跳, 求是否能跳出池子.
    比赛的时候一下子没读懂题意, 没看到是踩着鳄鱼往外跳…
    其实就是一个BFS

    赛后看到一个很好的图解

    AC代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <cmath>
    #define mst(a) memset(a, 0, sizeof(a))
    
    using namespace std;
    const int maxn = 105;
    int vis[maxn];
    int n, d;
    
    struct save007{
        int x, y;
        double dd;
    };
    save007 p[maxn];
    
    double dis( int x, int y ){
        return sqrt(x*x+y*y) - 15;
    }
    
    bool win( int x, int y ){
        if( x + d >= 50 || y + d >= 50 || x - d <= -50 || y - d <= -50 )
            return true;
        return false;
    }
    
    void BFS(){
        queue<save007> que;
        for( int i = 0; i < n; i++ ){
            if( p[i].dd <= d ){
                que.push(p[i]);
                vis[i] = 1;
            }
        }
        while( !que.empty() ){
            save007 node = que.front();
            if( win( node.x, node.y ) ){
                puts("Yes");
                return;
            }
            que.pop();
            for( int i = 0; i < n; i++ ){
                if( !vis[i] && node.dd + d >= p[i].dd ){
                    que.push(p[i]);
                    vis[i] = 1;
                }
            }
        }
        printf("No
    ");
        return;
    }
    
    void solve(){
        if( d >= 50 - 7.5 )  printf("Yes
    ");
        else BFS();
        return;
    }
    
    int main()
    {
        mst(vis);
        mst(p);
        scanf("%d%d",&n,&d);
        for( int i = 0; i < n; i++ ){
            scanf("%d%d",&p[i].x, &p[i].y);
            p[i].dd = dis(p[i].x, p[i].y);
        }
        solve();
        return 0;
    }
    
    
  • 相关阅读:
    Flask 入门
    Android studio 混淆打包
    Android----获取包名和sh1
    windows下 安装 rabbitMQ 及操作常用命令
    Nginx
    Linux安装mongoDB步骤和方法
    史上最简单的 SpringCloud 教程 | 终章
    idea 项目热部署设置
    JDK源码阅读顺序
    Linux常用命令
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740586.html
Copyright © 2020-2023  润新知