UVa 109 - SCUD Busters(凸包计算)
| 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
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 (
) 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
such that
, the signed area of the polygon is given by

where the x, y coordinates of
; the edges of the polygon are from
to
for
.
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.
推荐博客:http://www.cnblogs.com/devymex/archive/2010/08/09/1795391.html
解题思路:
计算几何类型的题目。需要用到三个基本算法,一是求凸包,二是判断点在多边形内,三是求多边形面积(题目中已给出)。关于凸包算法请详见Graham's Scan法。判断点在多边形内的算法有很多种,这里用到的是外积法:设待判断的点为p,逆时针或顺时针遍例多边形的每个点vn,将两个向量<p, vn>和<vn, vn + 1>做外积。如果对于多边形上所有的点,外积的符号都相同(顺时针为负,逆时针为正),则可断定p在多边形内。外积出现0,则表示p在边上,否则在多边形外。
算法的思路很直接,实现也很简单,关键是这道题的测试数据太扯蛋了,让我郁闷了很久。题目中并未说明导弹打在墙上怎么办,只是说“... whithin the wall ...”。根据测试结果来看,打在墙上和打在据点上都要算打中。题目中还提到国家不会互相重叠“... kingdoms do not overlap.”,但测试表明数据里确有重叠的情况,因此在导弹击中后一定要跳出循环,否则会出现一弹多击的情况。
给你N个王国,求下凸包,再求面积。给你一些炮弹,问炮弹炸掉的面积。(一个炮弹炸的话,整个王国都被炸了)。
直接求凸包后,求出各个王国的面积,然后判断炮弹在哪个王国里,这个直接用判断点是否在多边形内。
参考代码:
#include<bits/stdc++.h> using namespace std; const int MAX = ;
struct point{ double x,y;}; //点
struct polygon{ point c[MAX],a; double area; int n;};
struct segment{ point a,b;}; // 线段
const double eps = 1e-;
bool dy(double x,double y) { return x > y + eps;} // x > y
bool xy(double x,double y) { return x < y - eps;} // x < y
bool dyd(double x,double y) { return x > y - eps;} // x >= y
bool xyd(double x,double y) { return x < y + eps;} // x <= y
bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y
polygon king[MAX];
point c[MAX];
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
double disp2p(point a,point b)
{
return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
double area_polygon(point p[],int n)
{
double s = 0.0;
for(int i=; i<n; i++)
s += p[(i+)%n].y * p[i].x - p[(i+)%n].x * p[i].y;
return fabs(s)/2.0;
}
bool cmp(point a,point b) // 排序
{
double len = crossProduct(c[],a,b);
if( dd(len,0.0) )
return xy(disp2p(c[],a),disp2p(c[],b));
return xy(len,0.0);
}
bool onSegment(point a, point b, point c)
{
double maxx = max(a.x,b.x);
double maxy = max(a.y,b.y);
double minx = min(a.x,b.x);
double miny = min(a.y,b.y);
if( dd(crossProduct(a,b,c),0.0) && dyd(c.x,minx) && xyd(c.x,maxx) && dyd(c.y,miny) && xyd(c.y,maxy) )
return true;
return false;
}
bool segIntersect(point p1,point p2, point p3, point p4)
{
double d1 = crossProduct(p3,p4,p1);
double d2 = crossProduct(p3,p4,p2);
double d3 = crossProduct(p1,p2,p3);
double d4 = crossProduct(p1,p2,p4);
if( xy(d1 * d2,0.0) && xy(d3 * d4,0.0) ) return true;
if( dd(d1,0.0) && onSegment(p3,p4,p1) ) return true;//如果不判端点相交,则下面这四句话不需要
if( dd(d2,0.0) && onSegment(p3,p4,p2) ) return true;
if( dd(d3,0.0) && onSegment(p1,p2,p3) ) return true;
if( dd(d4,0.0) && onSegment(p1,p2,p4) ) return true;
return false;
}
bool point_inPolygon(point pot,point p[],int n)
{
int count = ;
segment l;
l.a = pot;
l.b.x = 1e10*1.0;
l.b.y = pot.y;
p[n] = p[];
for(int i=; i<n; i++)
{
if( onSegment(p[i],p[i+],pot) )
return true;
if( !dd(p[i].y,p[i+].y) )
{
int tmp = -;
if( onSegment(l.a,l.b,p[i]) )
tmp = i;
else
if( onSegment(l.a,l.b,p[i+]) )
tmp = i+;
if( tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)) )
count++;
else
if( tmp == - && segIntersect(p[i],p[i+],l.a,l.b) )
count++;
}
}
if( count % == )
return true;
return false;
}
point stk[MAX];
int top;
double Graham(int n)
{
int tmp = ;
for(int i=; i<n; i++)
if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) )
tmp = i;
swap(c[],c[tmp]);
sort(c+,c+n,cmp);
stk[] = c[]; stk[] = c[];
top = ;
for(int i=; i<n; i++)
{
while( xyd( crossProduct(stk[top],stk[top-],c[i]), 0.0 ) && top >= )
top--;
stk[++top] = c[i];
}
return area_polygon(stk,top+);
}
int main()
{
int n;
int i = ;
double x,y;
while( ~scanf("%d",&n) && n != - )
{
king[i].n = n;
for(int k=; k<n; k++)
scanf("%lf %lf",&king[i].c[k].x,&king[i].c[k].y);
king[i].a = king[i].c[];
i++;
} double sum = 0.0;
for(int k=; k<i; k++)
{
memcpy(c,king[k].c,sizeof(king[k].c));
king[k].area = Graham(king[k].n);
memcpy(king[k].c,stk,sizeof(stk));
king[k].n = top+;
}
point pot;
bool die[MAX];
memset(die,false,sizeof(die));
while( ~scanf("%lf %lf",&pot.x,&pot.y) )
{
for(int k=; k<i; k++)
if( point_inPolygon(pot,king[k].c,king[k].n) && !die[k] )
{
die[k] = true;
sum += king[k].area;
}
} printf("%.2lf\n",sum);
return ;
}
参考代码2:
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <vector>
#include <math.h> using namespace std; struct POINT {
int x; int y;
bool operator==(const POINT &other) {
return (x == other.x && y == other.y);
}
} ptBase; typedef vector<POINT> PTARRAY; bool CompareAngle(POINT pt1, POINT pt2) {
pt1.x -= ptBase.x, pt1.y -= ptBase.y;
pt2.x -= ptBase.x, pt2.y -= ptBase.y;
return (pt1.x / sqrt((float)(pt1.x * pt1.x + pt1.y * pt1.y)) <
pt2.x / sqrt((float)(pt2.x * pt2.x + pt2.y * pt2.y)));
} void CalcConvexHull(PTARRAY &vecSrc, PTARRAY &vecCH) {
ptBase = vecSrc.back();
sort(vecSrc.begin(), vecSrc.end() - , &CompareAngle);
vecCH.push_back(ptBase);
vecCH.push_back(vecSrc.front());
POINT ptLastVec = { vecCH.back().x - ptBase.x,
vecCH.back().y - ptBase.y };
PTARRAY::iterator i = vecSrc.begin();
for (++i; i != vecSrc.end() - ; ++i) {
POINT ptCurVec = { i->x - vecCH.back().x, i->y - vecCH.back().y };
while (ptCurVec.x * ptLastVec.y - ptCurVec.y * ptLastVec.x < ) {
vecCH.pop_back();
ptCurVec.x = i->x - vecCH.back().x;
ptCurVec.y = i->y - vecCH.back().y;
ptLastVec.x = vecCH.back().x - (vecCH.end() - )->x;
ptLastVec.y = vecCH.back().y - (vecCH.end() - )->y;
}
vecCH.push_back(*i);
ptLastVec = ptCurVec;
}
vecCH.push_back(vecCH.front());
} int CalcArea(PTARRAY &vecCH) {
int nArea = ;
for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - ; ++i) {
nArea += (i + )->x * i->y - i->x * (i + )->y;
}
return nArea;
} bool PointInConvexHull(POINT pt, PTARRAY &vecCH) {
for (PTARRAY::iterator i = vecCH.begin(); i != vecCH.end() - ; ++i) {
int nX1 = pt.x - i->x, nY1 = pt.y - i->y;
int nX2 = (i + )->x - i->x, nY2 = (i + )->y - i->y;
if (nX1 * nY2 - nY1 * nX2 < ) {
return false;
}
}
return true;
} int main(void) {
vector<PTARRAY> vecKingdom;
POINT ptIn;
int aFlag[] = {}, nAreaSum = ;
for (int nPtCnt; cin >> nPtCnt && nPtCnt >= ;) {
PTARRAY vecSrc, vecCH;
cin >> ptIn.x >> ptIn.y;
vecSrc.push_back(ptIn);
for (; --nPtCnt != ;) {
cin >> ptIn.x >> ptIn.y;
POINT &ptMin = vecSrc.back();
vecSrc.insert(vecSrc.end() - (ptIn.y > ptMin.y ||
(ptIn.y == ptMin.y && ptIn.x > ptMin.x)), ptIn);
}
CalcConvexHull(vecSrc, vecCH);
vecKingdom.push_back(vecCH);
}
while (cin >> ptIn.x >> ptIn.y) {
vector<PTARRAY>::iterator i = vecKingdom.begin();
for (int k = ; i != vecKingdom.end(); ++i, ++k) {
if (PointInConvexHull(ptIn, *i) && aFlag[k] != ) {
nAreaSum += CalcArea(*i);
aFlag[k] = ;
break;
}
}
}
cout << setiosflags(ios::fixed) << setprecision();
cout << (float)nAreaSum / 2.0f << endl;
return ;
}
UVa 109 - SCUD Busters(凸包计算)的更多相关文章
- POJ1264 SCUD Busters 凸包
POJ1264 有m个国家(m<=20)对每个国家给定n个城镇 这个国家的围墙是保证围住n个城镇的周长最短的多边形 必然是凸包 进行若干次导弹发射 落到一个国家内则国家被破坏 最后回答总共有多少 ...
- Win8 Metro(C#)数字图像处理--2.74图像凸包计算
原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...
- NAIPC 2019 A - Piece of Cake(凸包计算)
学习:https://blog.csdn.net/qq_21334057/article/details/99550805 题意:从nn个点中选择kk个点构成多边形,问期望面积. 题解:如果能够确定两 ...
- UVA 11168 Airport(凸包+直线方程)
题意:给你n[1,10000]个点,求出一条直线,让所有的点都在都在直线的一侧并且到直线的距离总和最小,输出最小平均值(最小值除以点数) 题解:根据题意可以知道任意角度画一条直线(所有点都在一边),然 ...
- POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
- UVA 10173 (几何凸包)
判断矩形能包围点集的最小面积:凸包 #include <iostream> #include <cmath> #include <cstdio> #include ...
- UVA 4728 Squares(凸包+旋转卡壳)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...
- UVA 10652 Board Wrapping(凸包)
The small sawmill in Mission, British Columbia, hasdeveloped a brand new way of packaging boards for ...
- Matrix Matcher UVA - 11019AC_自动机 + 代价提前计算
Code: #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...
随机推荐
- DDD:小议 BoundexContext 设计
背景 看了这篇文章:Coding for Domain-Driven Design: Tips for Data-Focused Devs,对 BoundedContext 的设计有了一点新的体会,记 ...
- Hadoop入门进阶课程4--HDFS原理及操作
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- IT人的自我导向型学习:开篇杂谈
报考大学时,家人让我报的是计算机系,那个时候,普遍都认为读计算机专业的人将来不用愁找不到工作.为何得出这样的结论不得而知,但是在过去三十年中,的确有很多响当当赚了大钱的IT人在影响着我们. 顺利的考取 ...
- Docker 定制容器镜像的2种方法
一.需求 由于在测试环境中使用了docker官网的centos 镜像,但是该镜像里面默认没有安装ssh服务,在做测试时又需要开启ssh.所以上网也查了查资料.下面详细的纪录下.在centos 容器内安 ...
- 自动化运维工具之 Ansible 介绍及安装使用
一.初识Ansible 介绍: Absible 使用 模块(Modules)来定义配置任务.模块可以用标准脚本语言(Python,Bash,Ruby,等等)编写,这是一个很好的做法,使每个模块幂等.A ...
- Eclipse魔法堂:任务管理器
一.前言 Eclipse的任务管理器为我们提供一个方便的入口查看工程代办事宜,并定位到对应的代码行继续之前的工作. 二.使用示例 示例1: /** * @Descripti ...
- Rest(Restful)风格的Web API跟RPC风格的SOAP WebService--这些名词都啥意思?
经常看到这些词汇,也有baidu或google过,但记忆里总是模糊,不确定,以至于别人问及的时候,总说不清楚.开篇随笔记录下.大家有补充或者意见的尽请留文. 本文顺序: 一.Rest(Restful) ...
- Scrum1.2--spring计划
项目功能--深入分析 燃尽图
- eclipse中去掉Js/javsscript报错信息
1.首先在problem>errors中删除所有js错误: 如下图 2.然后再勾选掉javascript Validator: 3.clean下项目吧,你会发现再也不出现js红叉叉了,哈哈.
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...