Uva 12171 Sculpture - 离散化 + floodfill

时间:2023-03-09 04:08:14
Uva 12171 Sculpture - 离散化 + floodfill

题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3323

题意:

三维空间中有n个长方体组成的雕塑,求表面积和体积。

分析:

因为长方体块可能在中间包围空气(也计入长方体体积),直接计算长方体体积是比较困难的,但是我们可以计算空气的体积,然后用总体积减去空气体积得出长方体的总体积。

这道题网上很多答案是模糊不清并且提交上去是错误的。

解这道题的关键是离散化后坐标的处理。在建立好离散坐标系后,利用离散坐标系在原坐标系中绘制长方体区域块,然后floodfill离散坐标系,初始点为(0,0,0)

注意这里的关键,floodfill处理的是离散坐标系! 比如离散坐标(0,0,0),他是原始坐标也是(0,0,0)  为原点, 长方体第一个顶点的原始坐标为(2,4,6),处理后的离散坐标可能是(1,1,1)也可能是(1,1,2)等,这个不要紧,bfs时判断其是不是长方体的顶点原始坐标(看看该离散坐标对应的原始坐标区域有无被标记)就好了,然后累加空气块体积和长方体的表面积即可。

附上一份网络上个人认为写得比较好的的参考代码:

 #include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; const int maxn = + ;
const int maxc = + ; int n, x0[maxn], y0[maxn], z0[maxn], x1[maxn], y1[maxn], z1[maxn]; int nx, ny, nz;
int xs[maxn*], ys[maxn*], zs[maxn*]; const int dx[] = {,-,,,,};
const int dy[] = {,,,-,,};
const int dz[] = {,,,,,-};
int color[maxn*][maxn*][maxn*]; struct Cell
{
int x, y, z;
Cell(int x=, int y=, int z=):x(x), y(y), z(z) {}
bool valid() const { return x >= && x < nx- && y >= && y < ny- && z >= && z < nz-;}
bool solid() const { return color[x][y][z] == ; }
bool getVis() const { return color[x][y][z] == ; }
void setVis() const { color[x][y][z] = ; }
Cell neighbor(int dir) const
{ return Cell(x+dx[dir], y+dy[dir], z+dz[dir]); }
int volume()
{ return (xs[x+]-xs[x]) * (ys[y+]-ys[y]) * (zs[z+]-zs[z]); }
int area(int dir)
{
if(dx[dir]) return (ys[y+]-ys[y]) * (zs[z+]-zs[z]);
if(dy[dir]) return (xs[x+]-xs[x]) * (zs[z+]-zs[z]);
return (xs[x+]-xs[x]) * (ys[y+]-ys[y]);
}
}; void discrectize(int* x, int& n)
{
sort(x, x + n);
n = unique(x, x + n) - x;
} int ID(int* x, int n, int x0)
{
return lower_bound(x, x + n, x0) - x;
} void floodfill(int& v, int& s)
{
v = s = ;
Cell c;
c.setVis();
queue<Cell> q;
q.push(c);
while(!q.empty())
{
Cell c = q.front(); q.pop();
v += c.volume();
for(int i = ; i < ; ++i)
{
Cell c2 = c.neighbor(i);
if(!c2.valid()) continue;
if(c2.solid()) s += c.area(i);
else if(!c2.getVis())
{
c2.setVis();
q.push(c2);
}
}
}
v = maxc*maxc*maxc - v;
} int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
memset(color, , sizeof(color));
nx = ny = nz = ;
xs[] = ys[] = zs[] = ;
xs[] = ys[] = zs[] = maxc;
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
scanf("%d%d%d%d%d%d", &x0[i], &y0[i], &z0[i], &x1[i], &y1[i], &z1[i]);
x1[i] += x0[i]; y1[i] += y0[i]; z1[i] += z0[i];
xs[nx++] = x0[i]; xs[nx++] = x1[i];
ys[ny++] = y0[i]; ys[ny++] = y1[i];
zs[nz++] = z0[i]; zs[nz++] = z1[i];
}
discrectize(xs, nx);
discrectize(ys, ny);
discrectize(zs, nz); for(int i = ; i < n; ++i)
{
int X1 = ID(xs, nx, x0[i]), X2 = ID(xs, nx, x1[i]);
int Y1 = ID(ys, ny, y0[i]), Y2 = ID(ys, ny, y1[i]);
int Z1 = ID(zs, nz, z0[i]), Z2 = ID(zs, nz, z1[i]);
for(int X = X1; X < X2; X++)
for(int Y = Y1; Y < Y2; ++Y)
for(int Z = Z1; Z < Z2; ++Z)
color[X][Y][Z] = ;
} int v, s;
floodfill(v, s);
printf("%d %d\n", s, v);
} return ;
}