UVALive 5908 Tracking RFIDs 【计算几何】

题意:给你一些电磁波发射器和一些接收器,发射的有效距离和中间的一些墙,电磁波每遇到一堵墙可传播距离就减少一,问每个接收器能收到哪些发射器的电磁波。
思路:由于发射器数据较大,接收器个数和有效距离数据较少,选择枚举接收器有效范围内的所有发射器,再通过线段相交来判断是否可收到。
优化:枚举发射器的时候,可以二分得到接收器的范围,在范围内枚举会减少计算量。

#pragma warning(disable:4996)
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
const double eps = 1e-8;
const int maxn = 250005;
struct Point
{
    int x,y;
    void in(){
        scanf("%d%d",&x,&y);
    }
    Point(){}
    Point(int _x,int _y):x(_x),y(_y){}
    Point operator +(const Point &t)const{
        return Point(x+t.x,y+t.y);
    }
    Point operator -(const Point &t)const{
        return Point(x-t.x,y-t.y);
    }
    bool operator < (const Point &a)const{
        return a.x == x? y<a.y : x<a.x;
    }
};
struct Line
{
    Point a,b;
    void in(){
        a.in(); b.in();
    }
};
int dbcmp(double n){
    return n<-eps ? -1:n>eps;
}
double getCross(Point o,Point a,Point b){
    double x1 = a.x - o.x; double x2 = b.x - o.x;
    double y1 = a.y - o.y; double y2 = b.y - o.y;
    return x1*y2 - x2*y1;
}
double getDot(Point o,Point a,Point b){
    double x1 = a.x - o.x; double x2 = b.x - o.x;
    double y1 = a.y - o.y; double y2 = b.y - o.y;
    return x1*x2 + y1*y2;
}
int lineXline(Point a,Point b,Point c,Point d){
    double d1 = getCross(a,b,c);    double d2 = getCross(a,b,d);
    double d3 = getCross(c,d,a);    double d4 = getCross(c,d,b);
    if(dbcmp(d1*d2) < 0 && dbcmp(d3*d4) < 0){
        return 1;
    } else {
        int cnt = 0;
        if (dbcmp(d1) == 0 && dbcmp(getDot(c,a,b)) <= 0)
            cnt++;
        if (dbcmp(d2) == 0 && dbcmp(getDot(d,a,b)) <= 0)
            cnt++;
        if (dbcmp(d3) == 0 && dbcmp(getDot(a,c,d)) <= 0)
            cnt++;
        if (dbcmp(d4) == 0 && dbcmp(getDot(b,c,d)) <= 0)
            cnt++;
        return cnt;
    }
}
double getDis(Point a,Point b){
    double dx = a.x-b.x;
    double dy = a.y-b.y;
    return sqrt(dx*dx+dy*dy);
}
Point pt[maxn];
Line Ln[maxn];
int S,R,W,P;
vector<Point>ans;
void solve(int x,int y){
    Point p = Point(x,y);
    Point left  = Point(x - R - 1,y);
    Point right = Point(x + R + 1,y);
    int l_id = lower_bound(pt,pt+S,left) - pt;
    int r_id = lower_bound(pt,pt+S,right) - pt;
    if (l_id == S)  {
        puts("0");return ;
    }
    ans.clear();
    for (int i = l_id; i < r_id; i++)
    {
        double ds = getDis(p,pt[i]);
        if(dbcmp(ds - R) > 0)continue;
        for (int j = 0;j<W;j++)
        {
            if(lineXline(pt[i],p,Ln[j].a,Ln[j].b))
                ds++;
        }
        if(dbcmp(ds - R) > 0)continue;
        ans.push_back(pt[i]);
    }
    sort(ans.begin(),ans.end());
    printf("%d",ans.size());
    for (int i = 0;i<ans.size();i++)
    {
        printf(" (%d,%d)",ans[i].x,ans[i].y);
    }
    puts("");
}
int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d%d%d%d",&S,&R,&W,&P);
        for (int i = 0;i < S;i++){
            pt[i].in();
        }
        sort(pt,pt+S);
        for (int i = 0;i < W;i++){
            Ln[i].in();
        }
        while (P--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            solve(x,y);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值