sgu290:Defend the Milky Way(三維凸包)

題目大意:
       給出空間中 n(1n100)  個點,求出其凸包。如果有點在凸包的面或棱上,也要將其算進凸包中,將答案按字典序輸出。

分析:
       首先我就不吐槽 sgu  坑爹的輸入了...
       主要就是求解三維凸包,至於凸包面或棱上的點可以在求出凸包後再加進去。凸包上每個面都是三角形,如果在平面中就類似三角剖分一般。
      
       這裏採用的是O(n2) 增量法,主要算法流程就是:
      (1)  初始化一個未退化的四面體;
      (2)  如果新點在凸包內或凸包上,忽視掉;
      (3)  如果新點在凸包外,刪去它可以看到的面,並將其併入整個凸包中。
      
       對於 (1)  我們要對三點共線,四點共面的情況分別判斷。
       對於 (2)  主要就是我們維護凸包上每個面的法向量朝外,通過對點積判斷正負來判斷是否能看到該面。對於由 A,B,C  構成的法向量爲 law  的三角形,如果點 P  能看到該面當且僅當 APlaw>0  ,如果一個面都看不到,那麼說明該點在凸包內。
       對於 (3)  就是把能看到的面刪去,同時將點 P  與凸包未被刪去的輪廓相連,也就是那些只被刪去過一次的邊。
       有圖有真相:
這裏寫圖片描述

AC code:

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <string>
#include <sstream>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#define vec pot
#define sqr(x) ((x)*(x))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define clr(a, b) memset(a, b, sizeof a)
#define rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define per(i, a, b) for(int i = (a); i >= (b); --i)
#define sqr(x) ((x)*(x))
typedef long long LL;
typedef double DB;
typedef long double LD;
using namespace std;

void open_init()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

void close_file()
{
    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
}

const int MAXN = 109;

int n;
string input;
string name[MAXN];
string ansn[MAXN];
int tot;
struct pot
{
    LL x, y, z;
    pot(LL x = 0, LL y = 0, LL z = 0):x(x),y(y),z(z){}
    friend pot operator + (const pot &a, const pot &b) 
    {
        return pot(a.x+b.x, a.y+b.y, a.z+b.z);
    }
    friend pot operator - (const pot &a, const pot &b)
    {
        return pot(a.x-b.x, a.y-b.y, a.z-b.z);
    }
    friend LL operator * (const pot &a, const pot &b) 
    {
        return a.x*b.x+a.y*b.y+a.z*b.z;
    }
    friend pot operator ^ (const pot &a, const pot &b) 
    {
        return pot(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
    }
}v[MAXN];
struct tri
{
    int p[4];
    pot law;
};
tri s[MAXN*MAXN];
int stot;
set<int> valid, ans;
int bel[MAXN][MAXN];
bool del[MAXN*MAXN];
queue<int> q;

inline void read(int &pos, LL &x)
{
    int tmp = pos, i, f = 1;x = 0;
    while(input[pos] == '-' || isdigit(input[pos])) pos--;
    i = pos+1, pos--;
    if(input[i] == '-') f = -1;
    else x = input[i]-'0';
    while(isdigit(input[++i])) x = x*10+input[i]-'0';
    x *= f;
}

inline bool zero(const pot &a)
{
    return !a.x && !a.y && !a.z;
}

inline bool collinear(int a, int b, int c)
{
    return zero((v[a]-v[b])^(v[a]-v[c]));
}

inline bool coplanar(int a, int b, int c, int d)
{
    return ((v[b]-v[a])^(v[c]-v[a]))*(v[d]-v[a]) == 0;
}

inline void end()
{
    cout << n << endl;
    sort(name+1, name+n+1);
    rep(i, 1, n)
        cout << name[i] << endl;
    exit(0);
}

pot law(int a, int b, int c)
{
    return (v[b]-v[a])^(v[c]-v[a]);
}

inline void add(int a, int b, int c)
{
    tri node;
    node.p[3] = node.p[0] = a, node.p[1] = b, node.p[2] = c;
    node.law = law(a, b, c);s[++stot] = node;valid.insert(stot);
    rep(i, 0, 2) bel[node.p[i]][node.p[i+1]] = stot;
}

inline void init(int a, int b, int c, int d)
{
    if(law(a, b, c)*(v[d]-v[a]) < 0) add(a, b, c);
    else add(a, c, b);
}

inline bool in(int a, int b, int c, int d)
{
    return ((v[b]-v[a])^(v[d]-v[a]))*((v[d]-v[a])^(v[c]-v[a])) >= 0;
}

inline bool in_tri(const tri &a, int b)
{
    int x(a.p[0]), y(a.p[1]), z(a.p[2]);
    if(!coplanar(a.p[0], a.p[1], a.p[2], b)) return false;
    return in(x, y, z, b) && in(y, z, x, b) && in(z, x, y, b);
}

int main()
{
    open_init();

    scanf("%d\n", &n);
    rep(i, 1, n)
    {
        char c;
        getline(cin, input);
        int j = input.size()-1;
        while((c=input[j]) == '\n' || c == '\r' || c == ' ') 
            j--;
        read(j, v[i].z), read(j, v[i].y), read(j, v[i].x);
        name[i] = input.substr(0, j+1);
    }
    if(n == 1) end();
    int ch1 = 0;
    rep(i, 3, n)
        if(!collinear(1, 2, i))
        {
            ch1 = i;
            break;
        }
    if(!ch1) end();
    int ch2 = 0;
    rep(i, 3, n)
        if(i != ch1 && !coplanar(1, 2, ch1, i))
        {
            ch2 = i;
            break;
        }
    if(!ch2) end();
    init(1, 2, ch1, ch2), init(1, 2, ch2, ch1);
    init(2, ch1, ch2, 1), init(1, ch1, ch2, 2);
    set<int>::iterator it;
    rep(i, 3, n)
        if(i != ch1 && i != ch2)
        {
            bool flag = false;
            for(it = valid.begin(); it != valid.end(); ++it)
                if(s[*it].law*(v[i]-v[s[*it].p[0]]) > 0)
                {
                    flag = true;
                    q.push(*it);
                    del[*it] = true;
                }
            if(!flag) continue;
            while(!q.empty())
            {
                int now = q.front();q.pop();
                valid.erase(now);
                rep(j, 0, 2)
                    if(!del[bel[s[now].p[j+1]][s[now].p[j]]])
                        add(i, s[now].p[j], s[now].p[j+1]);
            }
        }
    for(it = valid.begin(); it != valid.end(); ++it)
        rep(i, 0, 2) 
            ans.insert(s[*it].p[i]);
    rep(i, 1, n)
        if(!ans.count(i))
            for(it = valid.begin(); it != valid.end(); ++it)
                if(in_tri(s[*it], i))
                    ans.insert(i);
    for(it = ans.begin(); it != ans.end(); ++it)
        ansn[++tot] = name[*it];
    cout << tot << endl;
    sort(ansn+1, ansn+tot+1);
    rep(i, 1, tot)
        cout << ansn[i] << endl;

    close_file();
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章