24h購物| | PChome| 登入
2013-03-27 12:16:18| 人氣1,286| 回應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,286) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][四分樹] 806 - Spatial Structures
此分類上一篇:[UVA][sieve] 10168 - Summation of Four Primes

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文