UVa12171 hdu2771 UVaLive4291 Sculpture

填坑系列(p.171)

orz rjl

代码基本和rjl的一样

 

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

 

posted @ 2015-12-27 21:53  Showson  阅读(257)  评论(0编辑  收藏  举报