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> ...
随机推荐
- [IR] Information Extraction
阶段性总结 Boolean retrieval 单词搜索 [Qword1 and Qword2] O(x+y) [Qword1 and Qword2]- 改进: Gallo ...
- 哀悼我的第一次"创业"经历
下周考完最后一科,大学四年时光基本上算是落下帷幕,剩下的就只是整整毕业设计了.如果按照正常的节奏,这个学期应该能够搞完毕业设计的2/3,但无奈还在广州的一家公司里面实习,没有多少时间弄,得拖到3月了. ...
- sql server 随机读取数据
--sql server 随机读取数据 * FROM [tablename] ORDER BY NEWID() pk from [tablename] ORDER BY NEWID()) --这两个方 ...
- Mysql查找所有项目开始时间比之前项目结束时间小的项目ID
这是之前遇到过的一道sql面试题,供参考学习: 查找所有项目开始时间比之前项目结束时间小的项目ID mysql> select * from t2; +----+---------------- ...
- vs2015 Android SDK
It was not possible to complete an automatic installation. This might be due to a problem with your ...
- How to Convert Subversion Repo to Git
用了比较长时间的 SVN,但现在新的项目都采用Git.之前的项目又不得不维护,那么能不能将项目从SVN迁移到Git呢.答案是肯定的,网上的方案是 git-svn,或者更高级的封装 svn2git. 方 ...
- 关于PS激活的一些感想(附上PS CC2015)
最近跟着慕课学了一些前端的必备PS技能,就顺道把PS CC2015装上. 安装的过程没什么大问题,最要命的是激活环节!各种踩坑,但万万没想到,最终我还是成功的把它激活了. 本人所安装PS版本信息 好了 ...
- csharp: NHibernate and Entity Framework (EF) (object-relational mapper)
代码生成器: 1. http://www.codesmithtools.com/ 2.https://sourceforge.net/projects/mygeneration/ 3. http:// ...
- LGLDatePickerView
这个是封装 系统的PickerView 使用也比较简单, gihub地址:https://github.com/liguoliangiOS/LGLDatePickerView.git 效果图 使用方法 ...
- 【Asphyre引擎】学习笔记(一)
先来说说一下几个最基本的对象: TGraphicsDeviceProvider:这个对象决定我们的游戏是用什么来渲染的,比如DX或者OpenGL,DX还有多个版本可以选择. TCustomSwapCh ...