uva12171(floodfill)

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

抄的代码,,

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<queue>
  5 #include<cstdlib>
  6 #include<iostream>
  7 using namespace std;
  8 const int maxn=50+5;
  9 const int maxc=1000+10;
 10 int n,x0[maxn],x1[maxn],y0[maxn],y1[maxn],z0[maxn],z1[maxn];
 11 int nx,ny,nz;
 12 int xs[maxn<<1],ys[maxn<<1],zs[maxn<<1];
 13 const int dx[]={1,-1,0,0,0,0};
 14 const int dy[]={0,0,1,-1,0,0};
 15 const int dz[]={0,0,0,0,1,-1};
 16 int color[maxn<<1][maxn<<1][maxn<<1];
 17 struct CELL
 18 {
 19     int x,y,z;
 20     CELL(int x=0,int y=0,int z=0):x(x),y(y),z(z){}
 21     bool valid()const
 22     {
 23         return x>=0&&x<nx-1&&y>=0&&y<ny-1&&z>=0&&z<nz-1;
 24     }
 25     bool solid()const
 26     {
 27         return color[x][y][z]==1;
 28     }
 29     bool getvis()const
 30     {
 31         return color[x][y][z]==2;
 32     }
 33     void setvis() const
 34     {
 35         color[x][y][z]=2;
 36     }
 37     CELL neighbor(int dir) const
 38     {
 39         return CELL(x+dx[dir],y+dy[dir],z+dz[dir]);
 40     }
 41     int volume() const
 42     {
 43         return (xs[x+1]-xs[x])*(ys[y+1]-ys[y])*(zs[z+1]-zs[z]);
 44     }
 45     int area(int dir) const
 46     {
 47         if(dx[dir]!=0) return (ys[y+1]-ys[y])*(zs[z+1]-zs[z]);
 48         else if(dy[dir]!=0) return (zs[z+1]-zs[z])*(xs[x+1]-xs[x]);
 49         else if(dz[dir]!=0) return (xs[x+1]-xs[x])*(ys[y+1]-ys[y]);
 50     }
 51 
 52 };
 53 void discretize(int *x,int &n)
 54 {
 55     sort(x,x+n);
 56     n=unique(x,x+n)-x;
 57 }
 58 int ID(int *x,int n,int x0)
 59 {
 60     return lower_bound(x,x+n,x0)-x;
 61 }
 62 void floodfill(int &v,int &s)
 63 {
 64     v=0;s=0;
 65     CELL c;
 66     c.setvis();
 67     queue<CELL>q;
 68     q.push(c);
 69     while(!q.empty())
 70     {
 71         CELL c=q.front();
 72         q.pop();
 73         v+=c.volume();
 74         for(int i=0;i<6;i++)
 75         {
 76             CELL c2=c.neighbor(i);
 77             if(!c2.valid()) continue;
 78             if(c2.solid()) s+=c.area(i);
 79             else if(!c2.getvis())
 80             {
 81                 c2.setvis();
 82                 q.push(c2);
 83             }
 84         }
 85     }
 86     v=maxc*maxc*maxc-v;
 87 }
 88 int main()
 89 {
 90     int kase=0;
 91     scanf("%d",&kase);
 92     while(kase--)
 93     {
 94         nx=ny=nz=2;
 95         xs[0]=ys[0]=zs[0]=0;
 96         xs[1]=ys[1]=zs[1]=maxc;
 97         scanf("%d",&n);
 98         for(int i=0;i<n;i++)
 99         {
100             scanf("%d%d%d%d%d%d",&x0[i],&y0[i],&z0[i],&x1[i],&y1[i],&z1[i]);
101             x1[i]+=x0[i];y1[i]+=y0[i];z1[i]+=z0[i];
102             xs[nx++]=x0[i];xs[nx++]=x1[i];
103             ys[ny++]=y0[i];ys[ny++]=y1[i];
104             zs[nz++]=z0[i];zs[nz++]=z1[i];
105         }
106         discretize(xs,nx);
107         discretize(ys,ny);
108         discretize(zs,nz);
109 
110         memset(color,0,sizeof(color));
111         for(int i=0;i<n;i++)
112         {
113             int X1=ID(xs,nx,x0[i]);int X2=ID(xs,nx,x1[i]);
114             int Y1=ID(ys,ny,y0[i]);int Y2=ID(ys,ny,y1[i]);
115             int Z1=ID(zs,nz,z0[i]);int Z2=ID(zs,nz,z1[i]);
116             for(int X=X1;X<X2;X++)
117                 for(int Y=Y1;Y<Y2;Y++)
118                     for(int Z=Z1;Z<Z2;Z++)
119                     color[X][Y][Z]=1;
120         }
121         int v,s;
122         floodfill(v,s);
123         printf("%d %d\n",s,v);
124     }
125     return 0;
126 
127 
128 }

 

posted @ 2017-04-28 02:16  yijiull  阅读(143)  评论(0编辑  收藏  举报