您的位置:首页 > 其它

UVa688 Mobile Phone Coverage(扫描线法)

2014-09-17 09:53 204 查看
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 (xi, yi) with radius ri. However, in this city Maxnorm such a coverage region becomes the
square 

. In other words, the distance between two points (xp, yp)
and (xq, yq) is measured by the max norm

,
or, the 

 norm, in this city Maxnorm instead of the ordinary Euclidean norm

.

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 xi, yi, ri such that (xi, yi)
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.



The first integer n is the number of antennas, such that 

. The coordinate
of the i-th antenna is given by (xi, yi), and its power is ri. xi, yi 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.02
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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: