24h購物| | PChome| 登入
2012-05-12 08:47:03| 人氣753| 回應0 | 上一篇 | 下一篇

[UVA][ConvexHull] 109 - SCUD Busters

推薦 0 收藏 0 轉貼0 訂閱站台


 SCUD Busters 

Background

Some problems are difficult to solve but have a simplification that is easy to solve. Rather than deal with the difficulties of constructing a model of the Earth (a somewhat oblate spheroid), consider a pre-Columbian flat world that is a 500 kilometer tex2html_wrap_inline41 500 kilometer square.

In the model used in this problem, the flat world consists of several warring kingdoms. Though warlike, the people of the world are strict isolationists; each kingdom is surrounded by a high (but thin) wall designed to both protect the kingdom and to isolate it. To avoid fights for power, each kingdom has its own electric power plant.

When the urge to fight becomes too great, the people of a kingdom often launch missiles at other kingdoms. Each SCUD missile (Sanitary Cleansing Universal Destroyer) that lands within the walls of a kingdom destroys that kingdom's power plant (without loss of life).

The Problem

Given coordinate locations of several kingdoms (by specifying the locations of houses and the location of the power plant in a kingdom) and missile landings you are to write a program that determines the total area of all kingdoms that are without power after an exchange of missile fire.

In the simple world of this problem kingdoms do not overlap. Furthermore, the walls surrounding each kingdom are considered to be of zero thickness. The wall surrounding a kingdom is the minimal-perimeter wall that completely surrounds all the houses and the power station that comprise a kingdom; the area of a kingdom is the area enclosed by the minimal-perimeter thin wall.

There is exactly one power station per kingdom.

There may be empty space between kingdoms.

The Input

The input is a sequence of kingdom specifications followed by a sequence of missile landing locations.

A kingdom is specified by a number N ( tex2html_wrap_inline45 ) on a single line which indicates the number of sites in this kingdom. The next line contains the x and y coordinates of the power station, followed by N-1 lines of x, y pairs indicating the locations of homes served by this power station. A value of -1 for N indicates that there are no more kingdoms. There will be at least one kingdom in the data set.

Following the last kingdom specification will be the coordinates of one or more missile attacks, indicating the location of a missile landing. Each missile location is on a line by itself. You are to process missile attacks until you reach the end of the file.

Locations are specified in kilometers using coordinates on a 500 km by 500 km grid. All coordinates will be integers between 0 and 500 inclusive. Coordinates are specified as a pair of integers separated by white-space on a single line. The input file will consist of up to 20 kingdoms, followed by any number of missile attacks.

The Output

The output consists of a single number representing the total area of all kingdoms without electricity after all missile attacks have been processed. The number should be printed with (and correct to) two decimal places.

Sample Input

12
3 3
4 6
4 11
4 8
10 6
5 7
6 6
6 3
7 9
10 4
10 9
1 7
5
20 20
20 40
40 20
40 40
30 30
3
10 10
21 10
21 13
-1
5 5
20 12

Sample Output

70.50

A Hint

You may or may not find the following formula useful.

Given a polygon described by the vertices tex2html_wrap_inline61 such that tex2html_wrap_inline63 , the signed area of the polygon is given by

displaymath59

where the x, y coordinates of tex2html_wrap_inline65 ; the edges of the polygon are from tex2html_wrap_inline67 to tex2html_wrap_inline69 for tex2html_wrap_inline71 .

If the points describing the polygon are given in a counterclockwise direction, the value of a will be positive, and if the points of the polygon are listed in a clockwise direction, the value of a will be negative.



給最多 20 個凸包的王國, 接著給炸彈座標, 請問有多少面積的王國會被摧毀,
記住不要重覆計算到面積,
要判定一點有沒有在凸包內, 採用將該點放入一起做凸包, 然後做一次,
如果面積增加則在凸包外, 反之在內

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Point {
    int x, y;
};
Point P[20][200], CH[20][200], tCH[200], tP[200];
int CHm[20], Pn[20];
double A[20];
double area(Point CH[], int CHm, double &A) {
    int i;
    double sum = 0;
    for(i = 0; i < CHm-1; i++)
        sum += (CH[i].x*CH[i+1].y - CH[i].y*CH[i+1].x);
    A = sum/2;
}
int cmp(const void *i, const void *j) {
    Point *x, *y;
    x = (Point *)i, y = (Point *)j;
    if(x->x != y->x)
        return x->x - y->x;
    return x->y - y->y;
}
int cross(Point *o, Point *a, Point *b) {
    return (o->x - a->x)*(o->y - b->y) -
            (o->y - a->y)*(o->x - b->x);
}
void convexHull(int n, Point P[], Point CH[], int &CHm) {
    qsort(P, n, sizeof(Point), cmp);
    int m = 0, t, i;
    for(i = 0; i < n; i++) {
        while(m >= 2 && cross(&CH[m-2], &CH[m-1], &P[i]) <= 0)
            m--;
        CH[m++] = P[i];
    }
    for(i = n-1, t = m+1; i >= 0; i--) {
        while(m >= t && cross(&CH[m-2], &CH[m-1], &P[i]) <= 0)
            m--;
        CH[m++] = P[i];
    }
    CHm = m;
}
int main() {
    int N = 0, i, n;
    while(scanf("%d", &n) == 1) {
        if(n < 0)
            break;
        for(i = 0; i < n; i++) {
            scanf("%d %d", &P[N][i].x, &P[N][i].y);
        }
        convexHull(n, P[N], CH[N], CHm[N]);
        area(CH[N], CHm[N], A[N]);
        Pn[N] = n;
        N++;
    }
    double ans = 0, tarea;
    int x, y, used[20] = {}, m;
    Point tmp;
    while(scanf("%d %d", &tmp.x, &tmp.y) == 2) {
        for(i = 0; i < N; i++) {
            if(used[i])
                continue;
            memcpy(tP, P[i], sizeof(tP));
            tP[Pn[i]] = tmp;
            convexHull(Pn[i]+1, tP, tCH, m);
            area(tCH, m, tarea);
            if(tarea == A[i]) {
                used[i] = 1;
                ans += tarea;
            }
        }
    }
    printf("%.2lf\n", ans);
    return 0;
}

台長: Morris
人氣(753) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][path] 11015 - 05-2 Rendezvous
此分類上一篇:[UVA][String] 10062 - Tell me the frequencies!

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