UVa688 Mobile Phone Coverage(扫描线法)

本文介绍了一种用于计算多个矩形天线在特定城市中信号覆盖区域面积的算法。该算法利用了扫描线技术,通过处理天线的位置和功率来确定有效的覆盖范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A mobile phone company ACMICPC (Advanced Cellular, Mobile, and Internet-Connected Phone Corporation) is planning to set up a collection of antennas for mobile phones in a city called Maxnorm. The company ACMICPC has several collections for locations of antennas as their candidate plans, and now they want to know which collection is the best choice.


For this purpose, they want to develop a computer program to find the coverage of a collection of antenna locations. Each antenna Ai has power ri, corresponding to ``radius''. Usually, the coverage region of the antenna may be modeled as a disk centered at the location of the antenna (xiyi) with radius ri. However, in this city Maxnorm such a coverage region becomes the square $[x_i - r_i, x_i + r_i] \times [y_i - r_i, y_i + r_i]$. In other words, the distance between two points (xpyp) and (xqyq) is measured by the max norm$\max \{ \vert x_p - x_q\vert, \vert y_p - y_q\vert \}$, or, the $L_{\infty}$ norm, in this city Maxnorm instead of the ordinary Euclidean norm$\sqrt{(x_p - x_q)^2 + (y_p - y_q)^2}$.


As an example, consider the following collection of 3 antennas

4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0

depicted in the following figure

where the i-th row represents xiyiri such that (xiyi) is the position of the i-th antenna and ri is its power. The area of regions of points covered by at least one antenna is 52.00 in this case.


Write a program that finds the area of coverage by a given collection of antenna locations.

Input 

The input contains multiple data sets, each representing a collection of antenna locations. A data set is given in the following format.


\begin{displaymath}\begin{array}{lll}n & & \\x_1 & y_1 & r_1 \\x_2 & y_2 & r_2 \\\dots & & \\x_n & y_n & r_n\end{array}\end{displaymath}

The first integer n is the number of antennas, such that $2 Ÿ\le n Ÿ\le 100$. The coordinate of the i-th antenna is given by (xiyi), and its power is rixiyi and ri are fractional numbers between 0 and 200 inclusive.


The end of the input is indicated by a data set with 0 as the value of n.

Output 

For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the coverage region. The area should be printed with two digits to the right of the decimal point, after rounding it to two decimal places.


The sequence number and the area should be printed on the same line with no spaces at the beginning and end of the line. The two numbers should be separated by a space.

Sample Input 

3
4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0
2
3.0 3.0 3.0
1.5 1.5 1.0
0

Sample Output 

1 52.00
2 36.00

用到sweeping line算法

1、首先将所有矩形的左右两条边坐标从小到大排列,得到xPoint[0..n]

2、然后求在xPoint[i]到xPoint[i + 1]之间的矩形,中间可能有相交的,可能没有,涉及到合并的问题

3、将求得的小矩形面积一个一个的叠加

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstring>

using namespace std;

const int N = 110;
const double EPS = 1e-6;

struct Rect
{
    double x1;
	double x2;
	double y1;
	double y2;
};

struct Line
{
    double y1, y2;
    bool operator < (const Line &other) const
    {
        if (fabs(y1 - other.y1) > EPS) {
            return y1 < other.y1;
        }

        return y2 < other.y2;
    }
};

Rect rect[N];
int n;
int cas = 1;

bool input()
{
    scanf("%d", &n);
    if (n == 0) return false;

    for (int i = 0; i < n; i++) {
        double x, y, r;
        scanf("%lf%lf%lf", &x, &y, &r);
        rect[i].x1 = x - r;
        rect[i].x2 = x + r;
        rect[i].y1 = y - r;
        rect[i].y2 = y + r;
    }

    return true;
}

vector<Line> checky(double x1, double x2)
{
    vector<Line> ans;

    for (int i = 0; i < n; i++) {
        if (rect[i].x1 - x1 < EPS && rect[i].x2 - x2 > -EPS) {
            Line tmp;
            tmp.y1 = rect[i].y1;
            tmp.y2 = rect[i].y2;
            ans.push_back(tmp);
        }
    }

    sort(ans.begin(), ans.end());

    double start = 0, end = 0;
    if (!ans.empty()) {
        start = ans[0].y1;
        end = ans[0].y2;
    }

    vector<Line> result;

    for (size_t i = 1; i < ans.size(); i++) {
        if (ans[i].y1 - end < EPS) {
            end = max(end, ans[i].y2);
        } else {
            Line tmp;
            tmp.y1 = start;
            tmp.y2 = end;
            result.push_back(tmp);
            start = ans[i].y1;
            end = ans[i].y2;
        }
    }

    Line tmp;
    tmp.y1 = start;
    tmp.y2 = end;
    result.push_back(tmp);

    return result;
}

void solve()
{
    vector<double> vxPoint;

    for (int i = 0; i < n; i++) {
        vxPoint.push_back(rect[i].x1);
        vxPoint.push_back(rect[i].x2);
    }

    sort(vxPoint.begin(), vxPoint.end());

    vector<double>::iterator end = unique(vxPoint.begin(), vxPoint.end());
    vxPoint.erase(end, vxPoint.end());

    double area = 0;
    for (size_t i = 0; i + 1 < vxPoint.size(); i++) {
        double x1 = vxPoint[i], x2 = vxPoint[i + 1];
        vector<Line> vyLine = checky(x1, x2);

        for (size_t j = 0; j < vyLine.size(); j++) {
            double y1 = vyLine[j].y1, y2 = vyLine[j].y2;
            area += (y2 - y1) * (x2 - x1);
        }
    }

    printf("%d %.2f\n", cas++, area);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("d:\\OJ\\uva_in.txt", "r", stdin);
#endif

    while (input()) {
        solve();
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值