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> ...
随机推荐
- 【转载】解决 Subversion 的 “svn: Can't convert string from 'UTF-8' to native encoding” 错误
转载自:http://blog.csdn.net/shaohui/article/details/3996274 在google code 上创建了一个新的项目, 用Windows 下面的tortoi ...
- Python - 利用pip管理包
下载与安装setuptools和pip https://pypi.python.org/packages/source/s/setuptoolshttps://pypi.python.org/pypi ...
- 设计前沿:25款精妙的 iOS 应用程序图标
在这篇文章中,我为大家精心挑选的25款巧妙设计的 iOS 应用程序图标,会激发你未来的工作.苹果的产品总是让人爱不释手,设计精美,对用户使用体验把握得淋漓尽致,iPhone.iPad.iPod和 iM ...
- 基于Solr实现HBase的二级索引
文章来源:http://www.open-open.com/lib/view/open1421501717312.html 实现目的: 由于hbase基于行健有序存储,在查询时使用行健十分高效,然后想 ...
- redis配置文件
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => bytes # 1kb => ...
- mysql根据身份证信息来获取用户属性信息
需要:根据身份证信息来获取用户属性 方法:可以使用如下sql语句: ) ' then '北京市' ' then '天津市' ' then '河北省' ' then '山西省' ' then '内蒙古自 ...
- 怎样实现Web控件文本框Reset的功能
在ASP.NET开发过程序,在数据插入之后,文本框TextBox控件需要Reset.如果只有一两个文件框也许没有什么问题,如果网页上有很多文本框,你就会有点问题了.再加上某一情形,一些文本框是有默认值 ...
- .net xml 增删改查基础复习及干货分享
今天做做项目时,有一个需求需要用到一些固定的文本数据,觉得将这些需要存储的信息直接写在代码里很不友好,放在数据库中存储又觉得不够方便,自然就想到了使用xml来进行操作,我平常在项目中其实用到xml的机 ...
- display:inline-block兼容ie6/7的写法
2.display:inline-block作用? 使用display:inline-block属性,可以使行内元素或块元素能够变成行内块元素,简单直白点讲就是不加float属性就可以定义自身的宽.高 ...
- Studio for WPF:定制 C1WPFChart 标记
在本篇文章中,我们将阐述如何定制 C1WPFChart 数据点的标记. 下面分步讲解实现: 1.定制自定义样式: 1: <Window.Resources> 2: <Style x: ...