雷达检测-LintCode

204 篇文章 0 订阅

一个2D平面上有一堆雷达(雷达有x, y坐标,以及能探测到的范围r半径)。现在有一辆小车要从y = 0和y = 1的区间里面通过并且不能被雷达探测到。若被检测到,输出YES,否则输出NO。(可以认为,小车是一条长度为1的线段,沿直线从x = 0 向右前进)

 注意事项
雷达数量为n,n <= 1000。
雷达的坐标x为非负整数,y为整数,r为正整数。

样例
给出 coordinates = [[0,2]], radius = [1], 返回 “NO”。

解释:
在(0,2)处有个雷达,它能探测到以(0,2)为圆心,半径为1的圆形区域,小车不会被检测到。

给出 coordinates = [[0,2],[1,2]], radius = [1,2], 返回 “YES”。

解释:
在(0,2)处有个雷达,它能探测到以(0,2)为圆心,半径为2的圆形区域,(1,2)处的雷达能探测到以(1,2)为圆心,2为半径的圆形区域。2号雷达可以探测到小车经过。

思路
是否会检测到只和y的值有关。

#ifndef C957_H
#define C957_H
#include<iostream>
#include<string>
#include<vector>
using namespace std;
struct Point{
    int x;
    int y;
    Point() :x(0), y(0){}
    Point(int a, int b) :x(a), y(b){}
};
class Solution {
public:
    /**
    * @param coordinates: The radars' coordinate
    * @param radius: Detection radius of radars
    * @return: The car was detected or not
    */
    string radarDetection(vector<Point> &coordinates, vector<int> &radius) {
        // Write your code here
        if (coordinates.empty() || radius.empty())
            return "NO";
        int len = coordinates.size();
        for (int i = 0; i < len; ++i)
        {
            if (isDetected(coordinates[i].y, radius[i]))
            {
                return "YES";
            }
        }
        return "NO";
    }
    //判断是否会检测到,只与y坐标的值有关
    bool isDetected(int y, int radius)
    {
        if (y >= 0 && y <= 1)
            return true;
        else if (y > 1)
        {
            if (y - radius < 1)
                return true;
            else
                return false;
        }
        else
        {
            if (y + radius>0)
                return true;
            else
                return false;
        }
    }
};
#endif
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值