题目链接:uva 1030 - Image Is Everything
题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给出n,然后是该立体的前、左、后、右、上和下的视图,然后判断该立体的最大体积是多少。
解题思路:首先先把所有视图上为‘.'的地方清空,然后枚举视图上不为’.'的地方,计算对应的坐标第一个不为空得位置,将其涂色(注意,若一个正方体被着两种不同的颜色,说明该位置不存在正方体)。
#include <stdio.h>
#include <string.h> #define REP(i,n) for (int i = 0; i < (n); i++)
const int N = 15; int n;
char view[N][N][N], pos[N][N][N]; char getChar() {
char ch;
while (true) {
ch = getchar();
if ((ch >= 'A' && ch <= 'Z') || ch == '.') return ch;
}
} void input() {
REP(i, n) REP(k, 6) REP(j, n) view[k][i][j] = getChar();
REP(x, n) REP(y, n) REP(z, n) pos[x][y][z] = '#';
} void search(int i, int j, int k, int p, int& x, int& y, int& z) {
switch(k) {
case 0:
x = i, y = j, z = p; return;
case 1:
x = i, y = p, z = n - j - 1; return;
case 2:
x = i, y = n - j - 1, z = n - p - 1; return;
case 3:
x = i, y = n - p - 1, z = j; return;
case 4:
x = p, y = j, z = n - i - 1; return;
case 5:
x = n - p - 1, y = j, z = i; return;
}
} int solve() { int x, y, z; REP(k, 6) REP(i, n) REP(j, n)
if (view[k][i][j] == '.')
REP(p, n) {
search(i, j, k, p, x, y, z);
pos[x][y][z] = '.';
} while (true) {
bool flag = true;
REP(k, 6) REP(i, n) REP(j, n)
if (view[k][i][j] != '.')
REP(p, n) {
search(i, j, k, p, x, y, z);
if (pos[x][y][z] == '.') continue;
if (pos[x][y][z] == '#')
pos[x][y][z] = view[k][i][j]; if(pos[x][y][z] == view[k][i][j]) break;
pos[x][y][z] = '.';
flag = false;
}
if (flag) break;
} int ans = 0;
REP(x, n) REP(y, n) REP(z, n)
if (pos[x][y][z] != '.') ans++;
return ans;
} int main () {
while (scanf("%d", &n) == 1 && n) {
input();
printf("Maximum weight: %d gram(s)\n", solve() );
}
return 0;
}