UVa 12171 (discretized floodfill) sculpture

Source: Internet
Author: User

Test instructions

In three-dimensional space, there is a sculpture of n cuboid to find the surface area and volume.

Analysis:

We can add a circle of "air" to the outermost, then the volume of the connected block of air, and finally the volume of the sculpture by subtracting the total volume.

Another "serious" problem is that 5003 occupies too much space and therefore needs to be discretized. The original coordinates are used when calculating the volume and surface area.

After the discretization of the coordinates are saved in Xs, Ys, ZS, coordinates (x, y, z) of the lattice representatives ([xs[x], ys[y], zs[z]) ~ (Xs[x+1], ys[y+1], zs[z+1]) this small box.

The difficulty of the problem for me is probably more clear, but a lot of code details do not handle the kind.

It is a good technique to encapsulate nodes and related functions in a structure, which makes the coding idea clear and the code readable.

1#include <cstdio>2#include <algorithm>3#include <queue>4#include <cstring>5 using namespacestd;6 7 Const intMAXN = -+5;8 Const intMAXC = ++1;9 Ten intN, X0[maxn], Y0[MAXN], Z0[MAXN], X1[MAXN], Y1[MAXN], Z1[MAXN]; One  A intNX, NY, NZ; - intxs[maxn*2], ys[maxn*2], zs[maxn*2]; -  the Const intDx[] = {1,-1,0,0,0,0}; - Const intDy[] = {0,0,1,-1,0,0}; - Const intDz[] = {0,0,0,0,1,-1}; - intcolor[maxn*2][maxn*2][maxn*2]; +  - structCell + { A     intx, y, Z; atCell (intx=0,inty=0,intz=0): X (x), Y (y), Z (z) {} -     BOOLValid ()Const{returnX >=0&& x < nx-1&& y >=0&& y < ny-1&& Z >=0&& Z < nz-1;} -     BOOLSolid ()Const{returnColor[x][y][z] = =1; } -     BOOLGetvis ()Const{returnColor[x][y][z] = =2; } -     voidSetvis ()Const{Color[x][y][z] =2; } -Cell neighbor (intDirConst in{returnCell (X+dx[dir], Y+dy[dir], z+Dz[dir]); } -     intVolume () to{return(xs[x+1]-XS[X]) * (ys[y+1]-ys[y]) * (zs[z+1]-Zs[z]); } +     intAreaintdir) -     { the         if(Dx[dir])return(ys[y+1]-ys[y]) * (zs[z+1]-zs[z]); *         if(Dy[dir])return(xs[x+1]-XS[X]) * (zs[z+1]-zs[z]); $         return(xs[x+1]-XS[X]) * (ys[y+1]-ys[y]);Panax Notoginseng     } - }; the  + voidDiscrectize (int* x,int&N) A { theSort (x, X +n); +n = unique (x, x + N)-x; - } $  $ intID (int* x,intNintx0) - { -     returnLower_bound (x, x + N, x0)-x; the } - Wuyi voidFloodFill (int& V,int&s) the { -v = s =0; Wu Cell C; - C.setvis (); AboutQueue<cell>Q; $ Q.push (c); -      while(!q.empty ()) -     { -Cell C =Q.front (); Q.pop (); AV + =C.volume (); +          for(inti =0; I <6; ++i) the         { -Cell C2 =C.neighbor (i); $             if(!c2.valid ())Continue; the             if(C2.solid ()) s + =C.area (i); the             Else if(!C2.getvis ()) the             { the C2.setvis (); - Q.push (C2); in             } the         } the     } Aboutv = maxc*maxc*maxc-v; the } the  the intMain () + { -     //freopen ("In.txt", "R", stdin); the     intT;Bayiscanf"%d", &T); the      while(t--) the     { -memset (Color,0,sizeof(color)); -NX = NY = NZ =2; thexs[0] = ys[0] = zs[0] =0; thexs[1] = ys[1] = zs[1] =MAXC; thescanf"%d", &n); the          for(inti =0; I < n; ++i) -         { thescanf"%d%d%d%d%d%d", &x0[i], &y0[i], &z0[i], &x1[i], &y1[i], &z1[i]); theX1[i] + = X0[i]; Y1[i] + = Y0[i]; Z1[i] + =Z0[i]; thexs[nx++] = X0[i]; xs[nx++] =X1[i];94ys[ny++] = Y0[i]; ys[ny++] =Y1[i]; thezs[nz++] = Z0[i]; zs[nz++] =Z1[i]; the         } the Discrectize (XS, NX);98 Discrectize (Ys, NY); About discrectize (ZS, NZ); - 101          for(inti =0; I < n; ++i)102         {103             intX1 = ID (XS, NX, X0[i]), X2 =ID (XS, NX, X1[i]);104             intY1 = ID (ys, NY, Y0[i]), Y2 =ID (Ys, NY, Y1[i]); the             intZ1 = ID (ZS, NZ, Z0[i]), Z2 =ID (ZS, NZ, Z1[i]);106              for(intX = X1; X < X2; X + +)107                  for(intY = Y1; Y < Y2; ++Y)108                      for(intZ = Z1; Z < Z2; ++Z)109COLOR[X][Y][Z] =1; the         }111  the         intV, S;113 FloodFill (v, s); theprintf"%d%d\n", S, v); the     } the 117     return 0;118}
code June

UVa 12171 (discretized floodfill) sculpture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.