POJ 1264 SCUD Busters 计算几何

SCUD Busters
Time Limit: 1000MS		Memory Limit: 10000K
Total Submissions: 637		Accepted: 275
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


计算几何的综合题。在一张500x500的地图上有若干个王国,每个王国都有若干个供电站,王国的边界为包含所有供电站的最小多边形。给出数次导弹攻击的座标,每次导弹攻击会使这个王国中所有的供电站失效。求最后所有没电的王国总面积。

分为三步做:1. 计算凸包,2. 计算多边形面积,3. 判断点是否在多边形内。


1. 计算凸包

什么是凸包?(摘自维基百科):

在一个实数向量空间V中,对于给定集合X,所有包含X的凸集交集S被称为X凸包

 S := \bigcap_{X \subseteq K \subseteq V \atop K\ \mathrm{is\ convex}} K.

X的凸包可以用X内所有点(x_1, \ldots, x_n)线性组合来构造。

 S := \left\{ \left. \, \sum_{j=1}^n t_j x_j\, \right| x_j \in X,\, \sum_{j=1}^n t_j = 1,\, t_j \in \lbrack 0, 1 \rbrack \, \right\}.

在二维欧几里得空间中,凸包可想象为一条刚好包著所有点的橡皮圈。

简单的说,凸包即一个恰好包含所有点的,面积最小的多边形。

求凸包的方法之一:Graham扫描法。

从最下(若有多个取最左)的点A0开始,计算它与其他点的连线与X轴的角度,令dx=x1-x2, dy=y1-y2,则angle=acos(dx/sqrt(dx*dx+dy*dy))。

按angle的大小从小到大排序。设排序后的顶点序列为A1,A2,...,An.

令A0入队,从A1开始处理。设当前顶点为An

若队列仅有一个元素,则Ak入队;

若Ak-2,Ak-1,Ak所成的角向外(从向量Ak-1 - Ak-2的方向上观察An在向量左侧),则Ak入队;

若Ak-2,Ak-1,Ak所成的角向内,则弹出Ak-1,重新处理Ak。

最后队列中的所有顶点组成A0..An的凸包。


2. 计算多边形面积

设顶点为(x1,y1) (x2,y2) (x3,y3) ... (xn, yn)则多边形面积

S = 0.5 * (x1 * y2 + x2 * y3 + x3 * y4 + ... + xn-1 * yn + xn * y1 - x2 * y1 - x3 * y2 - x4 * y3 - ... - xn * yn-1 - x1 * yn)


3. 判断点是否在多边形内

设按逆时针排序的顶点A0, A1, ..., An,得向量序列A1-A0,A2-A1,A3-A2,...,An - An-1,

若点总在序列中向量的左侧,则点在多边形内;反之不在。


源代码:

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

struct point
{
	float x, y, angle;
};

bool cmpbycoord(const point &a, const point &b) { return (a.y == b.y) ? (a.x < b.x) : (a.y < b.y); }
bool cmpbyangle(const point &a, const point &b) { return a.angle < b.angle; }
float get_angle(float dx, float dy) { return acosf(dx / sqrt(dx * dx + dy * dy)); }
float cross_prod(float dx1, float dy1, float dx2, float dy2) { return dx1 * dy2 - dx2 * dy1; }

point input[100], kingdom[20][101];
int n, kn, km[20];
float area[20];
bool clear[20];

void graham()
{
	km[kn] = 0;
	sort(input, input + n, cmpbycoord);
	kingdom[kn][km[kn]++] = input[0];
	for(int i = 1; i < n; i++) {
		input[i].angle = get_angle(input[i].x - input[0].x, input[i].y - input[0].y);
	}
	sort(input + 1, input + n, cmpbyangle);
	kingdom[kn][km[kn]++] = input[1];
	for(int i = 2; i < n; i++) {
		if(cross_prod(
			kingdom[kn][km[kn] - 1].x - kingdom[kn][km[kn] - 2].x,
			kingdom[kn][km[kn] - 1].y - kingdom[kn][km[kn] - 2].y,
			input[i].x - kingdom[kn][km[kn] - 2].x,
			input[i].y - kingdom[kn][km[kn] - 2].y) >= 0) {
				kingdom[kn][km[kn]++]= input[i];
		} else if(km[kn] == 2) {
			kingdom[kn][km[kn] - 1] = input[i];
		} else {
			km[kn]--, i--;
		}
	}
	kingdom[kn][km[kn]++] = kingdom[kn][0];
}

void read()
{
	kn = 0;
	memset(clear, 0, sizeof(clear));
	while(~scanf("%d", &n)) {
		if(n == -1) return ;
		for(int i = 0; i < n; i++) {
			scanf("%f%f", &input[i].x, &input[i].y);
		}
		graham();
		area[kn] = 0.0f;
		for(int i = 0; i < km[kn] - 1; i++) {
			area[kn] += kingdom[kn][i].x * kingdom[kn][i + 1].y - kingdom[kn][i].y * kingdom[kn][i + 1].x;
		}
		area[kn] /= 2.0f;
		kn++;
	}
}

int in_kingdom(float x, float y)
{
	int p;
	for(int i = 0; i < kn; i++) {
		p = 0;
		for(int j = 0; j < km[i] - 1; j++) {
			if(cross_prod(kingdom[i][j + 1].x - kingdom[i][j].x, kingdom[i][j + 1].y - kingdom[i][j].y,
							x - kingdom[i][j].x, y - kingdom[i][j].y) >= 0) p++;
		}
		if(p == km[i] - 1) return i;
	}
	return -1;
}

void solve()
{
	float x, y;
	while(~scanf("%f%f", &x, &y)) {
		int k = in_kingdom(x, y);
		if(k != -1) {
			clear[k] = true;
		}
	}
	float ans = 0.0f;
	for(int i = 0; i < kn; i++) {
		if(clear[i]) ans += area[i];
	}
	printf("%.2f\n", ans);
}

int main()
{
	read();
	solve();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值