POJ 3528 Ultimate Weapon三维凸包(解题报告)

来源:互联网 发布:东方有线 有线通 知乎 编辑:程序博客网 时间:2024/04/28 06:10

Description

In year 2008 of the Cosmic Calendar, the Aliens send a huge armada towards the Earth seeking after conquest. The humans now depend on their ultimate weapon to retain their last hope of survival. The weapon, while capable of creating a continuous, closed and convex lethal region in the space and annihilating everything enclosed within, unfortunately exhausts upon each launch a tremendous amount of energy which is proportional to the surface area of the lethal region.

Given the positions of all battleships in the Aliens’ armada, your task is to calculate the minimum amount of energy required to destroy the armada with a single launch of the ultimate weapon. You need to report the surface area of the lethal region only.

Input

The first line contains one number N – the number of battleships.(1 ≤ N ≤ 500)
Following N lines each contains three integers presenting the position of one battleship.

Output

The minimal area rounded to three decimal places.

Sample Input

40 0 04 0 02 3 01 1 2

Sample Output

19.137

三维凸包的模版,找三维凸包的模版请在页面上搜 “三维凸包模版”:


#include<iostream>#include<stdio.h>#include<cmath>#include<cstring>#include<cstdlib>#include<algorithm>using namespace std;const int MAXN = 505;const double EPS = 1e-8;struct Point{double x,y,z;Point(){} Point (double xx,double yy,double zz):x(xx),y(yy),z(zz){} Point operator-(const Point p1){return Point(x-p1.x,y-p1.y,z-p1.z);} Point operator*(Point p){return Point(y*p.z-z*p.y,z*p.x-x*p.z,x*p.y-y*p.x);} double operator^(Point p){return (x*p.x+y*p.y+z*p.z);}}; struct CH3D{struct face{int a,b,c;bool ok;}; int n;Point P[MAXN]; int num;face F[8*MAXN]; int g[MAXN][MAXN]; double vlen(Point a){return sqrt(a.x*a.x+a.y*a.y+a.z*a.z);} Point cross(const Point &a,const Point &b,const Point &c){return Point((b.y-a.y)*(c.z-a.z)-(b.z-a.z)*(c.y-a.y),-((b.x-a.x)*(c.z-a.z)-(b.z-a.z)*(c.x-a.x)),(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x));}double area(Point a,Point b,Point c){return vlen((b-a)*(c-a));}double volume(Point a,Point b,Point c,Point d){return (b-a)*(c-a)^(d-a);}double dblcmp(Point &p,face &f){Point m=P[f.b]-P[f.a];Point n=P[f.c]-P[f.a];Point t=p-P[f.a];return (m*n)^t;}void deal(int p,int a,int b){int f=g[a][b];face add;if(F[f].ok){if(dblcmp(P[p],F[f])>EPS) dfs(p,f);else{add.a=b;add.b=a;add.c=p;add.ok=1;g[p][b]=g[a][p]=g[b][a]=num;F[num++]=add;}}} void dfs(int p,int now){F[now].ok=0;deal(p,F[now].b,F[now].a);deal(p,F[now].c,F[now].b);deal(p,F[now].a,F[now].c);} bool same(int s,int t){Point &a=P[F[s].a];Point &b=P[F[s].b];Point &c=P[F[s].c];return fabs(volume(a,b,c,P[F[t].a]))<EPS && fabs(volume(a,b,c,P[F[t].b]))<EPS && fabs(volume(a,b,c,P[F[t].c]))<EPS;} void solve(){int i,j,tmp;face add;bool flag = true;num=0;if(n<4){return ;} for(i=1;i<n;i++){if(vlen(P[0]-P[i])>EPS){swap(P[1],P[i]);flag=false;break;}}if(flag)return;flag = true;for(i=2;i<n;i++){if(vlen((P[0]-P[1])*(P[1]-P[i]))>EPS){swap(P[2],P[i]);flag=false;break;}}if(flag)return;flag=true;for(i=3;i<n;i++){if(fabs((P[0]-P[1])*(P[1]-P[2])^(P[0]-P[i]))>EPS) {swap(P[3],P[i]);flag=false;break;}}if(flag)return;for(i=0;i<4;i++){add.a=(i+1)%4;add.b=(i+2)%4;add.c=(i+3)%4;add.ok=true;if(dblcmp(P[i],add)>0){swap(add.b,add.c);}g[add.a][add.b]=g[add.b][add.c]=g[add.c][add.a]=num;F[num++]=add;}for(i=4;i<n;i++){for(j=0;j<num;j++){if(F[j].ok && dblcmp(P[i],F[j])>EPS){dfs(i,j);break;}}}tmp=num;for(i=num=0;i<tmp;i++){if(F[i].ok){F[num++]=F[i];}}}double area(){double res=0.0;if(n==3){Point p=cross(P[0],P[1],P[2]);res=vlen(p)/2.0;return res;}for(int i=0;i<num;i++){res+=area(P[F[i].a],P[F[i].b],P[F[i].c]);}return res/2.0;}};CH3D hull;int main(){int i;double res;scanf("%d",&hull.n);for(i=0;i<hull.n;i++){scanf("%lf%lf%lf",&hull.P[i].x,&hull.P[i].y,&hull.P[i].z);}hull.solve();res=hull.area();printf("%.3lf\n",res);return 0;}



原创粉丝点击