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> ...
随机推荐
- 如何准备阿里社招面试,顺谈 Java 程序员学习中各阶段的建议
引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试完以后,几乎能把自己和面试官的对话都给记下来.LZ自己当初面试完以后,除了记住一些聊过的知识点以外,具体的内容 ...
- Cocos2d-x网络通信
Cocos2d-x示例提供了三种内置的网咯通信类 HttpClient,WebSocket,SocketIO. 其中第一个是简单的HTTP协议的使用,提供很多Http请求方式. 剩下的Socket*是 ...
- Android学习笔记之ConnectivityManager+NetWorkInfo
PS:眼看就要开学了,该收收心了. 学习内容: 1.ConnecivityManager 2.NetWorkInfo ConnectivityManger:网络连接管理者,用于管理Android设 ...
- css中filter:alpha透明度使用
css中filter:alpha透明度使用 使用filter可以设置透明度,filter:alpha在IE下是没有问题的,要支持firefox就需要使用-moz-opacity,下面有个不错的示 ...
- mysql rand()产生随机整数范围及方法
根据官方文档,rand()的取值范围为[0,1) 若要在i ≤ R ≤ j 这个范围得到一个随机整数R ,需要用到表达式 FLOOR(i + RAND() * (j – i + 1))例如, 若要在7 ...
- jquery选择器(原创)
jquery选择器大方向可以分为这样: 下面我们先来看看基本选择器总的CSS选择器: 1.标签选择器: $("element") 其中,参数element,表示待查找的HTML标记 ...
- STL or 线段树 --- CSU 1555: Inversion Sequence
Inversion Sequence Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1555 Mean: 给你一 ...
- Worm.Win32.DownLoader.ns病毒主进程新式输入法注入分析(IME Inject)
1.病毒会在system32目录生成一个以tmp结尾的随机数命名的文件. 2.然后挂钩HOOK本进程空间的imm32.dll导出的ImmLoadLayout函数和ntdll.dll导出的ZwQuery ...
- HTML常用符号
HTML转义符号 HTML常用符号: 显示一个空格 < 小于 < <> 大于 > >& &符号 & &" 双引号 & ...
- iOS开发之蓝牙通讯
iOS开发之蓝牙通讯 一.引言 蓝牙是设备近距离通信的一种方便手段,在iPhone引入蓝牙4.0后,设备之间的通讯变得更加简单.相关的蓝牙操作由专门的CoreBluetooth.framework进行 ...