【题解】LA P2995【Image Is Everything】

分析

这又是一道模拟,当颜色不同时,可以删去一个立方体,并露出新的表面,直到不能删去为止。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define FOREACH(i,n) for(int i = 0;i < (n);i++)

const int MAXN = 10 + 6;
int n;
char pos[MAXN][MAXN][MAXN];
char view[6][MAXN][MAXN]; 

char read(){
    char s;
    while(true){
        s = getchar();
        if((s >= 'A' && s <= 'Z') || s == '.') return s;
    }
}

void get(int k,int i,int j,int len,int &x,int &y,int &z){
    if(k == 0) x = len,y = j,z = i;
    if(k == 1) x = n - 1 - j,y = len,z = i;
    if(k == 2) x = n - 1 - len,y = n - 1 - j,z = i;
    if(k == 3) x = j,y = n - 1 - len,z = i;
    if(k == 4) x = n - 1 - i,y = j,z = len;
    if(k == 5) x = i,y = j,z = n - 1 - len;
}

int main(){
    while(scanf("%d",&n) == 1 && n){
        FOREACH(i,n) FOREACH(k,6) FOREACH(j,n) view[k][i][j] = read();
        FOREACH(i,n) FOREACH(j,n) FOREACH(k,n) pos[i][j][k] = '#';

        FOREACH(k,6) FOREACH(i,n) FOREACH(j,n) if(view[k][i][j] == '.'){
            FOREACH(p,n){
                int x,y,z;
                get(k,i,j,p,x,y,z);
                pos[x][y][z] = '.';
            }
        }

        while(true){
            bool done = true;
            FOREACH(k,6) FOREACH(i,n) FOREACH(j,n) if(view[k][i][j] != '.'){
                FOREACH(p,n){
                    int x,y,z;
                    get(k,i,j,p,x,y,z);
                    if(pos[x][y][z] == '.') continue;
                    if(pos[x][y][z] == '#'){
                        pos[x][y][z] = view[k][i][j];
                        break;
                    }
                    if(pos[x][y][z] == view[k][i][j]) break;
                    pos[x][y][z] = '.';
                    done = false;
                }
            }
            if(done) break;
        }
        int ans = 0;
        FOREACH(i,n) FOREACH(j,n) FOREACH(k,n)
            if(pos[i][j][k] != '.') ans++;
        printf("Maximum weight: %d gram(s)\n",ans);
    }

    return 0;
}