uva 109 SCUD Busters
2016-08-13 14:53
435 查看
原题:
109 SCUD Bust ers
Background
Some problems are difficult to solve but h e 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 C leansing Univ ersal Destro y er) 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 lo cation 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 (3 ≤ N ≤ 100) 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 b y this p o w er station. A value of -1 for N indicates that there are no more kingdoms. There will be at least one kingdom in the dataset.
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.
Lo cations are specified in kilometers using coordinates on a 500 km b y 500 km grid. All coordinates will be integers bet een 0 and 500 inclusiv e. 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 y 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
中文:
有一堆国家,用一个个国家有很多区域和电厂,如果电厂被炸,国家就报销。这些国家用一个厚度忽略不计的墙围上,这些国家经常打仗,首先给一个数表示这个国家有多少个区域。输入一个-1表示国家输入完毕,接下来继续输入两个数字,表示导弹掉落的坐标,如果导弹掉落到这个国家里面,电厂就自动报销。现在问你被这些导弹轰炸以后,总共被毁掉的国家的面积和多少。
解答:
简单的计算几何问题,首先用凸包算法把所有凸包都求出来,然后保存。枚举,每输入一个导弹的坐标,枚举所有的国家,判断这个导弹是否掉入这个国家,注意,如果这个国家被轰炸过不能算第二次。把所以面积加一起即可。
109 SCUD Bust ers
Background
Some problems are difficult to solve but h e 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 C leansing Univ ersal Destro y er) 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 lo cation 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 (3 ≤ N ≤ 100) 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 b y this p o w er station. A value of -1 for N indicates that there are no more kingdoms. There will be at least one kingdom in the dataset.
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.
Lo cations are specified in kilometers using coordinates on a 500 km b y 500 km grid. All coordinates will be integers bet een 0 and 500 inclusiv e. 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 y 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
中文:
有一堆国家,用一个个国家有很多区域和电厂,如果电厂被炸,国家就报销。这些国家用一个厚度忽略不计的墙围上,这些国家经常打仗,首先给一个数表示这个国家有多少个区域。输入一个-1表示国家输入完毕,接下来继续输入两个数字,表示导弹掉落的坐标,如果导弹掉落到这个国家里面,电厂就自动报销。现在问你被这些导弹轰炸以后,总共被毁掉的国家的面积和多少。
#include <bits/stdc++.h> #define Vector Point using namespace std; const double PI=acos(-1); const double eps=1e-10; double torad(double deg) { return deg/180*PI; } struct Point { double x,y; Point(double x=0,double y=0):x(x),y(y){} }; double Dot(Vector A,Vector B) {return A.x*B.x+A.y*B.y;} bool operator < (const Point &a,const Point &b)//排序用,按照x的坐标从小到大,如果x相同,那么按照y从小到大 { return a.x<b.x||(a.x==b.x&&a.y<b.y); } Vector operator +(Vector A,Vector B){return Vector(A.x+B.x,A.y+B.y);}//坐标点相加 Vector operator -(Vector A,Vector B){return Vector(A.x-B.x,A.y-B.y);}//相减 double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } int ConvexHull(Point *p,int n,Point *ch)//p是所有点,n所有点的个数,ch里面记录形成凸包的点,返回凸包点的个数 { sort(p,p+n); int m=0; for(int i=0;i<n;i++) { while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0) m--; ch[m++]=p[i]; } if(n>1) m--; return m; } double PolyonArea(Point* p,int n)//多边形面积 { double area=0; for(int i=1;i<n-1;i++) area+=Cross(p[i]-p[0],p[i+1]-p[0]); return area/2; } int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } bool OnSegment(Point p,Point a1,Point a2) { return dcmp(Cross(a1-p,a2-p))==0&&dcmp(Dot(a1-p,a2-p))<0; } int isPointInPolygon(Point p,Point *ch,int n) { int wn=0; for(int i=0;i<n;i++) { if(OnSegment(p,ch[i],ch[(i+1)%n]))return -1; int k=dcmp(Cross(ch[(i+1)%n]-ch[i],p-ch[i])); int d1=dcmp(ch[i].y-p.y); int d2=dcmp(ch[(i+1)%n].y-p.y); if(k>0&&d1<=0&&d2>0) wn++; if(k<0&&d2<=0&&d1>0) wn--; } if(wn!=0) return 1; return 0; } int main() { ios::sync_with_stdio(false); int n,tot=0; Point p[101],ch[101][101],tmp[101],pp; int coun[101],use[101]; double area=0; memset(use,0,sizeof(use)); while(cin>>n) { if(n==-1) break; for(int i=0;i<n;i++) cin>>p[i].x>>p[i].y; int ind=ConvexHull(p,n,tmp); for(int i=0;i<ind;i++) ch[tot][i]=tmp[i]; coun[tot]=ind; tot++; } while(cin>>pp.x>>pp.y) { for(int i=0;i<tot;i++) { if(isPointInPolygon(pp,ch[i],coun[i])&&!use[i]) { area+=PolyonArea(ch[i],coun[i]); use[i]=1; } } } cout<<fixed<<setprecision(2)<<area<<endl; return 0; }
解答:
简单的计算几何问题,首先用凸包算法把所有凸包都求出来,然后保存。枚举,每输入一个导弹的坐标,枚举所有的国家,判断这个导弹是否掉入这个国家,注意,如果这个国家被轰炸过不能算第二次。把所以面积加一起即可。
相关文章推荐
- UVa 109 SCUD Busters (凸包面积&判断点是否在凸包内部)
- UVA 109 SCUD Busters【凸包模拟题】
- UVA 109 || SCUD Busters(凸包面积计算
- UVa 109 - SCUD Busters(凸包计算)
- UVa 109 - SCUD Busters(凸包)
- uva 109 SCUD Busters
- poj 1264 || UVA 109 SCUD Busters
- UVa 109 - SCUD Busters
- UVa Problem 109 - SCUD Busters
- uva 109 SCUD Busters-AC-Upgraded version
- [POJ] [1264] [SCUD Busters]
- uva109求凸包面积,判断点是不是在凸包内
- 入门经典-习题7-1,109-uva208消防车-DFS,并查集,打印路径,利用set保存边,字典序,STL⭐⭐⭐⭐⭐复杂度:3
- uva_109_SCUD Busters
- POJ 1264 SCUD Busters (凸包面积+判断点是否在凸包内)
- POJ 1264 SCUD Busters 计算几何
- POJ 1264 UVA 109 简单的计算几何
- poj 1264 hdu 1616 SCUD Busters 凸包+面积计算
- HOJ 1157 SCUD Busters
- Hoj 1157 SCUD Busters