题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=45

 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 xy 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(凸包计算)的更多相关文章

  1. POJ1264 SCUD Busters 凸包

    POJ1264 有m个国家(m<=20)对每个国家给定n个城镇 这个国家的围墙是保证围住n个城镇的周长最短的多边形 必然是凸包 进行若干次导弹发射 落到一个国家内则国家被破坏 最后回答总共有多少 ...

  2. Win8 Metro(C#)数字图像处理--2.74图像凸包计算

    原文:Win8 Metro(C#)数字图像处理--2.74图像凸包计算 /// <summary> /// Convex Hull compute. /// </summary> ...

  3. NAIPC 2019 A - Piece of Cake(凸包计算)

    学习:https://blog.csdn.net/qq_21334057/article/details/99550805 题意:从nn个点中选择kk个点构成多边形,问期望面积. 题解:如果能够确定两 ...

  4. UVA 11168 Airport(凸包+直线方程)

    题意:给你n[1,10000]个点,求出一条直线,让所有的点都在都在直线的一侧并且到直线的距离总和最小,输出最小平均值(最小值除以点数) 题解:根据题意可以知道任意角度画一条直线(所有点都在一边),然 ...

  5. POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心

    题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...

  6. UVA 10173 (几何凸包)

    判断矩形能包围点集的最小面积:凸包 #include <iostream> #include <cmath> #include <cstdio> #include ...

  7. UVA 4728 Squares(凸包+旋转卡壳)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17267 [思路] 凸包+旋转卡壳 求出凸包,用旋转卡壳算出凸包的直 ...

  8. UVA 10652 Board Wrapping(凸包)

    The small sawmill in Mission, British Columbia, hasdeveloped a brand new way of packaging boards for ...

  9. Matrix Matcher UVA - 11019AC_自动机 + 代价提前计算

    Code: #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...

随机推荐

  1. bootstrap插件学习-bootstrap.popover.js

    先看bootstrap.popover.js的结构 var Popover = function ( element, options ){} //构造器 Popover.prototype = {} ...

  2. 【转】sql server开启全文索引方法

    利用系统存储过程创建全文索引的具体步骤: 1) 启动数据库的全文处理功能 (sp_fulltext_database) 2) 建立全文目录 (sp_fulltext_catalog) 3) 在全文目录 ...

  3. [Bootstrap]7天深入Bootstrap(2)整体架构

    大多数Bootstrap的使用者都认为Bootstrap只提供了CSS组件 和JavaScript插件,其实CSS组件和JavaScript插件只是Bootstrap框架的表现形式而已,它们都是构建在 ...

  4. 设计模式--简单工厂(Factory)模式

    温故而知新,看了以前写的博文<Asp.net读取Excel文件 2>http://www.cnblogs.com/insus/archive/2011/05/05/2037808.html ...

  5. JS实现注销功能

    JS实现注销功能,代码如下: <script> window.history.forward(1); </script> 这个代码的用法就是: 比如,我们此时有两个页面:Log ...

  6. 重新想象 Windows 8 Store Apps (58) - 微软账号

    [源码下载] 重新想象 Windows 8 Store Apps (58) - 微软账号 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 微软账号 获取微软账号的用户 ...

  7. 使用layout_weight设置控件占屏幕百分比

    水平LinearLayout中如果A,B两个控件都是layout_weight="1",那么控件在水平方向占比为A的layout_width+1/2空闲空间,B的layout_wi ...

  8. java之内的工具分享,附带下载链接,方便以后自己寻找

    class反编译工具:http://pan.baidu.com/s/1geYvX5L redis客户端工具:http://pan.baidu.com/s/1eRJ4ThC mysql客户端-[mysq ...

  9. java之StringBuilder类详解

    StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...

  10. 51Node 1364--- 最大字典序排列(树状数组)

    51Node  1364--- 最大字典序排列(树状数组) 1364 最大字典序排列 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 给出一个1至N ...