POJ 3528--Ultimate Weapon(三维凸包)
| Time Limit: 2000MS | Memory Limit: 131072K | |
| Total Submissions: 2430 | Accepted: 1173 |
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
4
0 0 0
4 0 0
2 3 0
1 1 2
Sample Output
19.137
Hint
Source
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int N = ;
const double eps = 1e-;
typedef struct point3 {
double x, y, z;
point3() { }
point3(double a, double b, double c) :x(a), y(b), z(c) { }
point3 operator -(const point3 &b)const { //返回减去后的新点
return point3(x - b.x, y - b.y, z - b.z);
}
point3 operator +(const point3 &b)const { //返回加上后的新点
return point3(x + b.x, y + b.y, z + b.z);
}
//数乘计算
point3 operator *(const double &k)const { //返回相乘后的新点
return point3(x * k, y * k, z*k);
}
point3 operator /(const double &k)const { //返回相除后的新点
return point3(x / k, y / k, z / k);
}
double operator *(const point3 &b)const { //点乘
return (x*b.x + y*b.y + z*b.z);
}
point3 operator ^(const point3 &p) const { //叉积
return point3(y*p.z - p.y*z, z*p.x - x*p.z, x*p.y - y*p.x);
}
double vlen()const { //向量的模
return sqrt(x*x + y*y + z*z);
}
}point3;
struct fac {
int a, b, c;//凸包一个面上的三个点的编号
bool ok; //该面是否是最终凸包中的面
};
struct T3dhull {
int n; //初始点数
point3 ply[N]; //初始点
int trianglecnt; //凸包上三角形数
fac tri[N]; //凸包三角形可证明被创建的面不会超过6N
int vis[N][N]; //点i到点j是属于哪个面
double dist(point3 a) { return sqrt(a.x*a.x + a.y*a.y + a.z*a.z); } //两点长度
double area(point3 a, point3 b, point3 c){return dist((b - a) ^ (c - a));} //三角形面积*2 //返回四面体有向体积*6
//在储存面时,保证面的法线方向朝向凸包外部,如果在某一平面和点p所组成的四面体的有向体积为正,则p点在凸包外部,并且此点可以被p点看见。
double volume(point3 a, point3 b, point3 c, point3 d){
return ((b - a) ^ (c - a))* (d - a);
}
double ptoplane(point3 &p, fac &f){ //点到平面距离,体积法
point3 m = ply[f.b] - ply[f.a], n = ply[f.c] - ply[f.a], t = p - ply[f.a];
return (m^n) * t;
}
void deal(int p, int a, int b) {
int f = vis[a][b];
fac add;
if (tri[f].ok)
{
if ((ptoplane(ply[p], tri[f])) > eps)
dfs(p, f);
else
{
add.a = b, add.b = a, add.c = p, add.ok = ;
vis[p][b] = vis[a][p] = vis[b][a] = trianglecnt;
tri[trianglecnt++] = add;
}
}
}
void dfs(int p, int cnt) {//维护凸包,如果点p在凸包外侧则更新凸包
tri[cnt].ok = ;
deal(p, tri[cnt].b, tri[cnt].a);
deal(p, tri[cnt].c, tri[cnt].b);
deal(p, tri[cnt].a, tri[cnt].c);
}
bool same(int s, int e) {
point3 a = ply[tri[s].a], b = ply[tri[s].b], c = ply[tri[s].c];
return fabs(volume(a, b, c, ply[tri[e].a])) < eps
&& fabs(volume(a, b, c, ply[tri[e].b])) < eps
&& fabs(volume(a, b, c, ply[tri[e].c])) < eps;
}
void construct()//构造凸包
{
int i, j;
trianglecnt = ;
if (n<) return;
bool tmp = true;
for (i = ; i < n; i++) //前两点不共点
{
if ((dist(ply[] - ply[i])) > eps)
{
swap(ply[], ply[i]);
tmp = false;
break;
}
}
if (tmp)return;
tmp = true;
for (i = ; i < n; i++) //前三点不共线
{
if ((dist((ply[] - ply[]) ^ (ply[] - ply[i]))) > eps)
{
swap(ply[], ply[i]);
tmp = false;
break;
}
}
if (tmp) return;
tmp = true;
for (i = ; i < n; i++) //前四点不共面
{
if (fabs(((ply[] - ply[]) ^ (ply[] - ply[]))* (ply[] - ply[i]))>eps)
{
swap(ply[], ply[i]);
tmp = false;
break;
}
}
if (tmp)return;
fac add;
for (i = ; i < ; i++) //构建初始四面体
{
add.a = (i + ) % , add.b = (i + ) % , add.c = (i + ) % , add.ok = ;
if ((ptoplane(ply[i], add))>)
swap(add.b, add.c);
vis[add.a][add.b] = vis[add.b][add.c] = vis[add.c][add.a] = trianglecnt;
tri[trianglecnt++] = add;
}
for (i = ; i < n; i++) //构建更新凸包
{
for (j = ; j < trianglecnt; j++)
{
if (tri[j].ok && (ptoplane(ply[i], tri[j])) > eps)
{
dfs(i, j); break;
}
}
}
int cnt = trianglecnt;
trianglecnt = ;
for (i = ; i < cnt; i++)
{
if (tri[i].ok)
tri[trianglecnt++] = tri[i];
}
}
double area() //表面积
{
double ret = ;
for (int i = ; i < trianglecnt; i++)
ret += area(ply[tri[i].a], ply[tri[i].b], ply[tri[i].c]);
return ret / 2.0;
}
double volume()
{
point3 p(, , );
double ret = ;
for (int i = ; i < trianglecnt; i++)
ret += volume(p, ply[tri[i].a], ply[tri[i].b], ply[tri[i].c]);
return fabs(ret / );
}
}hull; int main() {
while (~scanf("%d", &hull.n)) {
int i;
for (i = ; i < hull.n; i++)
scanf("%lf %lf %lf", &hull.ply[i].x, &hull.ply[i].y, &hull.ply[i].z);
hull.construct();
printf("%.3lf\n", hull.area());
}
return ;
}
POJ 3528--Ultimate Weapon(三维凸包)的更多相关文章
- poj 3528 Ultimate Weapon (3D Convex Hull)
3528 -- Ultimate Weapon 一道三维凸包的题目,题目要求求出三维凸包的表面积.看懂了网上的三维凸包的代码以后,自己写的代码,跟网上的模板有所不同.调了一个晚上,结果发现错的只是数组 ...
- POJ 3528 求三维凸包表面积
也是用模板直接套的题目诶 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include < ...
- POJ 2225 / ZOJ 1438 / UVA 1438 Asteroids --三维凸包,求多面体重心
题意: 两个凸多面体,可以任意摆放,最多贴着,问他们重心的最短距离. 解法: 由于给出的是凸多面体,先构出两个三维凸包,再求其重心,求重心仿照求三角形重心的方式,然后再求两个多面体的重心到每个多面体的 ...
- 三维凸包求其表面积(POJ3528)
Ultimate Weapon Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 2074 Accepted: 989 D ...
- hdu4273Rescue(三维凸包重心)
链接 模板题已不叫题.. 三维凸包+凸包重心+点到平面距离(体积/点积) 体积-->混合积(先点乘再叉乘) #include <iostream> #include<cstd ...
- hdu4449Building Design(三维凸包+平面旋转)
链接 看了几小时也没看懂代码表示的何意..无奈下来问问考研舍友. 还是考研舍友比较靠谱,分分钟解决了我的疑问. 可能三维的东西在纸面上真的不好表示,网上没有形象的题解,只有简单"明了&quo ...
- hdu 4273 2012长春赛区网络赛 三维凸包中心到最近面距离 ***
新模板 /* HDU 4273 Rescue 给一个三维凸包,求重心到表面的最短距离 模板题:三维凸包+多边形重心+点面距离 */ #include<stdio.h> #include&l ...
- HDU 4573 Throw the Stones(动态三维凸包)(2013 ACM-ICPC长沙赛区全国邀请赛)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4573 Problem Description Remember our childhood? A fe ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
随机推荐
- 2013年未之wpf项目乱述
不知识为何现已很少在网上发帖,貌似人生的方向已经看的七七八八.要么用心工作,要么自主创业.无论怎么样,对于现在的我来说都是一种淡定的选择.作为一个c#程序员,今年下半年开始使用wpf,更觉得wpf将来 ...
- 项目经验:GIS<MapWinGIS>建模第七天
终天完成了管网地图的附加功能..实现了了管网与地图结合
- IE8下的怪异模式
使用DWZ框架,老是出现点击button后在winxp IE8下出现新页面,经过检查后发现IE8下submit后,return false就不行了,必须使用window.event.returnVal ...
- Excel 函数使用
字符串 20180613 转为日期 2018-06-13,单元格内输入如下公式 =DATE(LEFT(),MID(,),RIGHT()) IF 函数内的或.与 =IF(AND(A=B,C=D),&q ...
- IERS-OSPF基本工作原理
IERS-OSPF基本工作原理 一.邻居建立建立过程 1.Router ID 用于在自治系统中唯一标识一台运行OSPF的路由器,每台运行OSPF的路由器都有一个ROUTER ID Route ID 是 ...
- Oracle 内存使用建议性能视图
下面三个查询结果均可查询出随着内存参数设置的变化性能的变化情况,对oracle数据库内存的设置有一定的建议和指导作用. select t.SGA_SIZE,t.ESTD_DB_TIME_FACTOR ...
- GO语言(八) defer注意点
package main import ( "net" "os" "fmt" "io/ioutil" ) func Cl ...
- Entity Framework: 主从表的增删改
1.根据主表添加从表数据 var dest = (from d in context.Destinations where d.Name == "Bali" select d).S ...
- AngularJs学习笔记--I18n/L10n
原版地址:http://code.angularjs.org/1.0.2/docs/guide/i18n 一.I18n and L10n in AngularJS 1. 什么是I18n和L10n? 国 ...
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
原版地址:http://code.angularjs.org/1.0.2/docs/guide/di 一.Dependency Injection(依赖注入) 依赖注入(DI)是一个软件设计模式,处理 ...