POJ 1264 SCUD Busters (凸包面积+判断点是否在凸包内)

77 篇文章 8 订阅
22 篇文章 0 订阅
SCUD Busters
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1104 Accepted: 439

Description

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 x 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 (anitary leansing niversal estroyer) that lands within the walls of a kingdom destroys that kingdom's power plant (without loss of life).

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.

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 (3 <= N <= 100) 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.

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

Source

Duke Internet Programming Contest 1991,UVA 109

题目链接:http://poj.org/problem?id=1264

题目大意:简单的说就是给若干个点集,每个点集对应一个凸包,然后给若干个点,若点在某个凸包内(包含边界),则加上这个凸包的面积,同一个凸包面积只能加一起

题目分析:求凸包直接Graham扫描,面积用叉积求,判断点是否在某个凸包内也直接通过叉积搞,找一个基点(左下),然后从这个基点向凸包上的其他顶点引边,将凸包分成了若干个三角形,若所要判断的点p在凸包内,则先找到它所在的那个三角形,极角排序是逆时针,则对于第i个点必有cross(0, i, p) >= 0且cross(0, i + 1, p) <= 0,画个图就能很清楚看出,然后若p在三角形内,则必有cross(j, p, j + 1) <= 0,对于每个计算过的面积,标记下属于哪个凸包即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 105;
int n;
struct POINT {
    int x, y;
}base, p[MAX];

struct AREA {
    int top;
    double area;
    POINT stk[MAX];
}a[25];
bool vis[25];

void getBase() {  
    scanf("%d %d", &p[0].x, &p[0].y);  
    base.x = p[0].x;  
    base.y = p[0].y;  
    int pos = 0;  
    for (int i = 1; i < n; i ++) {  
        scanf("%d %d", &p[i].x, &p[i].y);  
        if (p[i].y < base.y || (p[i].y == base.y && p[i].x < base.x)) {  
            base.x = p[i].x;  
            base.y = p[i].y;  
            pos = i;  
        }  
    }  
    swap(p[0], p[pos]);  
}  

int getDist(POINT p1, POINT p2) {
    return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
}

int getCross(POINT p0, POINT p1, POINT p2) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

bool cmp(POINT p1, POINT p2) {
    if (getCross(base, p1, p2) == 0) {
        return getDist(base, p1) < getDist(base, p2);
    }
    if (getCross(base, p1, p2) > 0) {
        return true;
    }
    return false;
}

void getConvex(int i) {
    sort(p + 1, p + n, cmp);
    a[i].stk[0] = p[0];
    a[i].stk[1] = p[1];
    a[i].top = 1;
    for (int j = 2; j < n; j ++) {
        while (a[i].top > 0 && getCross(a[i].stk[a[i].top - 1], a[i].stk[a[i].top], p[j]) <= 0) {
            a[i].top --;
        }
        a[i].stk[++ a[i].top] = p[j];
    }
}

void getArea(int i) {
    a[i].stk[++ a[i].top] = p[0];
    for (int j = 0; j < a[i].top; j ++) {
        a[i].area += getCross(base, a[i].stk[j], a[i].stk[j + 1]);
    }
    a[i].area /= 2.0;
}

int main() {
    int cnt = 0;
    while(scanf("%d", &n) && n != -1) {
        getBase();
        getConvex(cnt);
        getArea(cnt);
        cnt ++;
    }
    POINT t;
    double ans = 0;
    while(scanf("%d %d", &t.x, &t.y) != EOF) {
        bool flag = false;
        for (int i = 0; i < cnt && !flag; i ++) {
            if (!vis[i]) {
                for (int j = 0; j < a[i].top && !flag; j ++) {
                    if (getCross(a[i].stk[0], a[i].stk[j], t) >= 0 &&  getCross(a[i].stk[0], a[i].stk[j + 1], t) <= 0) {
                        if (getCross(a[i].stk[j], t, a[i].stk[j + 1]) <= 0) {
                            vis[i] = true;
                            flag = true;
                            ans += a[i].area;
                        }
                    }
                } 
            }
        }
    } 
    printf("%.2f\n", ans);
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值