PTA - 拯救007 ( BFS )

在老电影“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\n");
    return;
}

void solve(){
    if( d >= 50 - 7.5 )  printf("Yes\n");
    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;
}

转载于:https://www.cnblogs.com/JinxiSui/p/9740586.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值