题解 UVA12171 【雕塑 Sculpture】

· · 题解

离散化加bfs
(dfs)很可能爆栈

我们可以开g[1010][1010][1010]的地图
把雕塑染上1
然后bfs统计空气的体积,和空气遇到雕塑的面积
答案体积v=总集体-空气体积
但是电脑太垃圾了,g[1010][1010][1010]太大 有1e9个int,而且耗时
竞赛一般只能开3千万个int

所以用离散化
离散化就是把原来的雕塑映射到新坐标g[110][110][110]
只有不到50个雕塑,所以新的g开的范围可以小很多

比如
x=465 y=150 z=994
x=199 y=566 z=465
x=1465 y=1535 z=13541
这三个点
xn={465,199,1465}
yn={150,566,1535}
zn={994,465,13541}
先排个序
xn={199,465,1465}
yn={150,566,1535}
zn={465,994,13541}

这三个点可以用
x=2 y=1 z=2
x=1 y=2 z=1
x=3 y=3 z=3
来表示

在看了网上的解释和刘汝佳的代码后
具体实现可以这样理解
新数组g里面,g[x0][y0][z0]的值表示一个小网格的状态,不同数值表示空气或者雕塑
x=x0,y=y0,z=z0表示一个点,用这个点来表示小网格,它是小网格四个顶点中里原点最近的顶点 x=x0+1,y=y0+1,z=z0+1表示这个小网格的对顶点
x=xn[x0],y=yn[y0],z=zn[z0]表示这个点原坐标
该网格体积=(xn[x0+1]-xn[x0])(yn[y0+1]-yn[y0])(zn[z0+1]-zn[z0]);
当空气网格碰到雕塑,若雕塑在其前面或后面(总共上下左右前后6个面),则统计雕塑面积s+=(yn[y0+1]-yn[y0])*(zn[z0+1]-zn[z0]);

我是看刘汝佳的代码理解后独立编写的 先贴他的

// UVa12171 Sculpture
// Rujia Liu
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int maxn = 50 + 5;
const int maxc = 1000 + 1;

// original data
int n, x0[maxn], y0[maxn], z0[maxn], x1[maxn], y1[maxn], z1[maxn];

// discretization related
int nx, ny, nz;
int xs[maxn*2], ys[maxn*2], zs[maxn*2];

// floodfill related
const int dx[] = {1,-1,0,0,0,0};
const int dy[] = {0,0,1,-1,0,0};
const int dz[] = {0,0,0,0,1,-1};
int color[maxn*2][maxn*2][maxn*2];

struct Cell {
  int x, y, z;
  Cell(int x=0, int y=0, int z=0):x(x),y(y),z(z){}
  bool valid() const {
    return x >= 0 && x < nx-1 && y >= 0 && y < ny-1 && z >= 0 && z < nz-1;
  }
  bool solid() const {
    return color[x][y][z] == 1; // solid
  }
  bool getVis() const {
    return color[x][y][z] == 2; // visited
  }
  void setVis() const {
    color[x][y][z] = 2;
  }
  Cell neighbor(int dir) const {
    return Cell(x+dx[dir], y+dy[dir], z+dz[dir]);
  }
  int volume() const {
    return (xs[x+1]-xs[x])*(ys[y+1]-ys[y])*(zs[z+1]-zs[z]);
  }
  int area(int dir) const {
    if(dx[dir] != 0) return (ys[y+1]-ys[y])*(zs[z+1]-zs[z]);
    else if(dy[dir] != 0) return (xs[x+1]-xs[x])*(zs[z+1]-zs[z]);
    return (xs[x+1]-xs[x])*(ys[y+1]-ys[y]);
  }
};

void discretize(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 = 0;
  s = 0;
  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 = 0; i < 6; 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() {
     #ifndef ONLINE_JUDGE
    freopen("cin1.txt","r",stdin);
    freopen("cout1.txt","w",stdout);
    #endif
  int T;
  scanf("%d", &T);
  while(T--) {
    nx = ny = nz = 2;
    xs[0] = ys[0] = zs[0] = 0;
    xs[1] = ys[1] = zs[1] = maxc;
    scanf("%d", &n);
    for(int i = 0; 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];
    }
    discretize(xs, nx);
    discretize(ys, ny);
    discretize(zs, nz);

    // paint
    memset(color, 0, sizeof(color));
    printf("lenx=%d leny=%d lenz=%d\n",nx,ny,nz);
    for(int i = 0; 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]);
      printf("X0=%d X1=%d\n",X1,X2);
      printf("Y0=%d Y1=%d\n",Y1,Y2);
            printf("Z0=%d Z1=%d\n",Z1,Z2);
     // system("pause");
      for(int X = X1; X < X2; X++)
        for(int Y = Y1; Y < Y2; Y++)
          for(int Z = Z1; Z < Z2; Z++)
            {
                printf("x=%d y=%d z=%d\n",X,Y,Z);
                color[X][Y][Z] = 1;
            }

    }

    int v, s;
    floodfill(v, s);
    printf("%d %d\n", s, v);
  }

  return 0;
}

下面是我的

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define r read()
using namespace std;
inline int read()
{
    bool ok=0 ;int res=0;char c;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-') ok=1;else res=c-48;
    while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+(c-48);
    return ok ? ~res + 1:res;
}
int t,n;
int dx[10]={1,-1,0,0,0,0};
int dy[10]={0,0,1,-1,0,0};
int dz[10]={0,0,0,0,1,-1};
int g[110][110][110];
struct node
{
    int x,y,z;

}c1,c2;
int x0[55],x1[55],y0[55],y1[55],z0[55],z1[55];
int xn[110],yn[110],zn[110],lenx,leny,lenz;
int main()
{
 #ifndef ONLINE_JUDGE
    freopen("cin.txt","r",stdin);
    freopen("cout.txt","w",stdout);
    #endif
    t=r;
    while(t--)
    {
        memset(g,0,sizeof(g));
        memset(xn,0,sizeof(xn));
        memset(yn,0,sizeof(yn));
        memset(zn,0,sizeof(zn));
        n=r;
        lenx=leny=lenz=0;
        //xn[++lenx]=0;yn[++leny]=0;zn[++lenz]=0;
        xn[++lenx]=1001;yn[++leny]=1001;zn[++lenz]=1001;
        for(int i=1;i<=n;i++)
        {
            x0[i]=r;y0[i]=r;z0[i]=r;
            x1[i]=r;y1[i]=r;z1[i]=r;
            x1[i]+=x0[i]; y1[i]+=y0[i]; z1[i]+=z0[i];
            xn[++lenx]=x0[i];xn[++lenx]=x1[i];
            yn[++leny]=y0[i];yn[++leny]=y1[i];
            zn[++lenz]=z0[i];zn[++lenz]=z1[i];
        }
    //  printf("prelenx1=%d\n",lenx);
        sort(xn,xn+1+lenx);
        lenx=unique(xn,xn+1+lenx)-(xn+1);//
    //  printf("prelenx2=%d\n",lenx);
        sort(yn,yn+1+leny);
        leny=unique(yn,yn+1+leny)-(yn+1);//

        sort(zn,zn+1+lenz);
        lenz=unique(zn,zn+1+lenz)-(zn+1);//

    //  printf("lenx=%d leny=%d lenz=%d\n",lenx,leny,lenz);
        for(int i=1;i<=n;i++)
        {
            int X0=lower_bound(xn,xn+1+lenx,x0[i])-(xn),X1=lower_bound(xn,xn+1+lenx,x1[i])-(xn);
            int Y0=lower_bound(yn,yn+1+leny,y0[i])-(yn),Y1=lower_bound(yn,yn+1+leny,y1[i])-(yn);
            int Z0=lower_bound(zn,zn+1+lenz,z0[i])-(zn),Z1=lower_bound(zn,zn+1+lenz,z1[i])-(zn);

        //  printf("X0=%d X1=%d\n",X0,X1);
        //  printf("Y0=%d Y1=%d\n",Y0,Y1);
        //  printf("Z0=%d Z1=%d\n",Z0,Z1);
            for(int x=X0;x<X1;x++)
            for(int y=Y0;y<Y1;y++)
            for(int z=Z0;z<Z1;z++)
            {
                g[x][y][z]=1;//solid 
            //  printf("x=%d y=%d z=%d\n",x,y,z);
            }
        }

        c1.x=0;c1.y=0;c1.z=0;
        queue<node>q;
        g[0][0][0]=2;//vised
        int v,s;
        v=0;s=0;

        q.push(c1);
        while(!q.empty())
        {
            c1=q.front();
            q.pop();
            v+=(xn[c1.x+1]-xn[c1.x])*(yn[c1.y+1]-yn[c1.y])*(zn[c1.z+1]-zn[c1.z]);
            for(int i=0;i<6;i++)
            {
                c2.x=c1.x+dx[i]; 
                c2.y=c1.y+dy[i];
                c2.z=c1.z+dz[i];
                if(c2.x>=0&&c2.x<lenx&&c2.y>=0&&c2.y<leny&&c2.z>=0&&c2.z<lenz)
                {
                    if(g[c2.x][c2.y][c2.z]==1)
                    {
                        if(dx[i]!=0) s+=(yn[c1.y+1]-yn[c1.y])*(zn[c1.z+1]-zn[c1.z]);
                        else if(dy[i]!=0) s+=(xn[c1.x+1]-xn[c1.x])*(zn[c1.z+1]-zn[c1.z]);
                        else s+=(xn[c1.x+1]-xn[c1.x])*(yn[c1.y+1]-yn[c1.y]);
                    }else if(g[c2.x][c2.y][c2.z]==0)
                    {
                        g[c2.x][c2.y][c2.z]=2;
                        q.push(c2);
                    }

                }
            }
        }
        //
        printf("%d %d\n",s,xn[lenx]*yn[leny]*zn[lenz]-v);

    }

    return 0;
}