uva12171 离散化

区间离散化,将1000*1000*1000缩小为最多100*100*100的空间,进行bfs;

#include <cstdio>
#include <string>
#include <iostream>
#include <cstring>
#include <set>
#include <cmath>
#include <algorithm>
#define LL long long
#include <map>
#include <vector>
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
const LL MA = 999 * 999 * 999;
struct Node
{
    int x1, y1, z1;
    int x2, y2, z2;
    void GetNode(int a, int b, int c, int d, int e, int f)
    {
        x1 = a; y1 = b; z1 = c;
        x2 = d; y2 = e; z2 = f;
    }

}No[55];
set<int>Set[3]; //x, y, z轴数据集合
int Map[3][105]; // 离散化
int ii[3];
int NumMap[3][1005];
bool G[105][105][105];
bool has[105][105][105];
LL ans1, ans2;
bool is_ok(int a, int b, int c)
{
    return a >= 0 && a < ii[0] && b >= 0 && b < ii[1] && c >= 0 && c < ii[2];
}
void bfs(int x, int y, int z)
{
    if(has[x][y][z]) return;
    has[x][y][z] = true;
    if(!G[x][y][z]) ans1 += (Map[0][x+1] - Map[0][x]) * (Map[1][y+1] - Map[1][y]) * (Map[2][z+1] - Map[2][z]);
    int m1[6] = {0, 0, 0, 0, 1, -1};
    int m2[6] = {0, 0, -1, 1, 0, 0};
    int m3[6] = {-1, 1, 0, 0, 0, 0};
    for(int i = 0; i < 6; i++)
    {
        int a = x + m1[i];
        int b = y + m2[i];
        int c = z + m3[i];
        if(is_ok(a, b, c))
        {
            if(!G[x][y][z] && G[a][b][c])
            {
                LL bb = 0;
                if(m1[i]) bb = abs(Map[1][y+1] - Map[1][y]) * 1LL * abs(Map[2][z+1] - Map[2][z]);
                if(m2[i]) bb = abs(Map[0][x+1] - Map[0][x]) * 1LL * abs(Map[2][z+1] - Map[2][z]);
                if(m3[i]) bb = abs(Map[1][y+1] - Map[1][y]) * 1LL * abs(Map[0][x+1] - Map[0][x]);
//                cout << "bb = " << bb << endl;
                ans2 += bb;
            }
            if(!G[a][b][c])
            bfs(a, b, c);
        }
    }
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n;
        mst(No, 0);
        ans1 = 0;
        ans2 = 0;
        for(int i = 0; i < 3; i++)
        {
            mst(Map[i], 0);
            mst(NumMap[i], 0);
            Set[i].clear();
        }
        for(int i = 0; i < 105; i++)
            for(int j = 0; j < 105; j++)
            {
                mst(G[i][j], false);
                mst(has[i][j], false);
            }
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            int a, b, c, d, e, f;
            scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);
            No[i].GetNode(a, b, c, a+d, b+e, c+f);
            Set[0].insert(a);Set[0].insert(a+d);
            Set[1].insert(b);Set[1].insert(b+e);
            Set[2].insert(c);Set[2].insert(c+f);
        }
        for(int i = 0; i < 3; i++)
        {
            ii[i] = 1;
            Map[i][0] = 1;
            for(set<int>::iterator it = Set[i].begin(); it != Set[i].end(); it++)
            {
                Map[i][ii[i]] = *it;
                NumMap[i][*it] = ii[i];
                ii[i]++;
            }
            Map[i][ii[i]] = 1000;
        }
//        //测试输出
//        for(int i = 0; i < 3; i++)
//            for(int j = 0; j <= ii[i]; j++)
//            printf("%d%c", Map[i][j], j == ii[i] ? '
' : ' ');

        for(int i0 = 0; i0 < n; i0++)
        {
            Node u = No[i0];
            for(int i = NumMap[0][u.x1]; i < NumMap[0][u.x2]; i++)
            {
                for(int j = NumMap[1][u.y1]; j < NumMap[1][u.y2]; j++)
                {
                    for(int k = NumMap[2][u.z1]; k < NumMap[2][u.z2]; k++)
                    {
                        G[i][j][k] = true;
                    }
                }
            }
        }
        bfs(0, 0, 0);
        cout << ans2 << " " << MA - ans1 << endl;
    }
    return 0;

}
View Code
原文地址:https://www.cnblogs.com/wolf-yasen/p/7526760.html