[UVA][矩形集] 870 - Intersecting Rectangles@Morris' Blog|PChome Online 人新台
2013-03-27 12:16:18| 人1,410| 回0 | 上一篇 | 下一篇

[UVA][矩形集] 870 - Intersecting Rectangles

0 收藏 0 0 站台

Intersecting Rectangles

Background

Consider, in a 2D space, a set of rectangles with edges that are parallel to the axis X and Y. One is the main rectangle and there are intersections between several rectangles. According to these intersections, one or more closed polygons called "islands" can be defined. The main island is the one that contains the main rectangle.


Figure 1 - The ten rectangles are defining 3 islands

The proposed problem is to evaluate the area of the main island. For example, in Figure 1, considering that polygon 1 is the main polygon, the main island is composed by polygons 1,2,3,5 and 10, so the problem here is to evaluate the area of the polygon presented in Figure 2.


Figure 2 - The polygon defined by the main island

Problem

Given a list of rectangles, starting with the main rectangle, the problem is to identify the main island and evaluate the correspondent area. Each rectangle is geometrically characterized by the coordinates of its bottom-left and upper-right corners; the maximum number of rectangles is 100; as a simplification, it can be considered that no coincidences between edges and vertices exist.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The first line of the input contains the number of rectangles N (integer format) to be considered. Each one of the following N lines contains the coordinates of bottom-left and upper-right vertices of a rectangle, in the sequence xmin ymin xmax ymax, separated by a single space. The first rectangle given is the main one.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

One integer number that represents the main island area as defined above.


Figure 3 - Sample input

Sample Input

1

9
2 5 8 8
4 6 10 10
5 7 6 12
9 9 11 11 12 7 15 8
1 3 3 6
4 2 8 3
13 6 14 9
2 1 11 4

Sample Output

70 



University of Porto ACM Programming Contest / Round 12/2000

求矩形的即面,一函判矩形之有有相交。

利用散化再次去算面。

#include <stdio.h>
#include <algorithm>
using namespace std;

int hasIntersect(int lx, int ly, int rx, int ry,
                int la, int lb, int ra, int rb) {
    lx = max(lx, la);
    ly = max(ly, lb);
    rx = min(rx, ra);
    ry = min(ry, rb);
    return lx < rx && ly < ry;
}
int main() {
    int t, n, i, j, k;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        int lx[105], ly[105], rx[105], ry[105];
        int X[305], Y[305], m = 0;
        for(i = 0; i < n; i++) {
            scanf("%d %d %d %d", &lx[i], &ly[i], &rx[i], &ry[i]);
            X[m] = lx[i], Y[m] = ly[i];
            m++;
            X[m] = rx[i], Y[m] = ry[i];
            m++;
        }
        sort(X, X+m);
        sort(Y, Y+m);
        int Q[105], Qt = 0, used[105] = {};
        Q[Qt] = 0, used[0] = 1;
        for(i = 0; i <= Qt; i++) {
            k = Q[i];
            for(j = 0; j < n; j++) {
                if(used[j]) continue;
                if(hasIntersect(lx[k], ly[k], rx[k], ry[k], lx[j], ly[j], rx[j], ry[j])) {
                    Q[++Qt] = j;
                    used[j] = 1;
                }
            }
        }
        n = Qt;
        int ans = 0;
        for(i = 1; i < m; i++) {
            for(j = 1; j < m; j++) {
                for(k = 0; k <= n; k++) {
                    if(X[i-1] >= lx[Q[k]] && X[i] <= rx[Q[k]] &&
                       Y[j-1] >= ly[Q[k]] && Y[j] <= ry[Q[k]]) {
                        break;
                    }
                }
                if(k != n+1) {
                    ans += (X[i]-X[i-1])*(Y[j]-Y[j-1]);
                }
            }
        }
        printf("%d\n", ans);
        if(t)
            puts("");
    }
    return 0;
}

台: Morris
人(1,410) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][四分] 806 - Spatial Structures
此分上一篇:[UVA][sieve] 10168 - Summation of Four Primes

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86