这个题先找出“看穿”的所有方块,拿下来,再不断找每一块在各个视图中颜色不一样的给删掉,直到没有为止,本题难点在于各个视图和小方块的坐标转化,还是看看刘汝佳的代码,太屌了,我用了两种转化,他一种就搞出来了,以下是我的代码:
#include<stdio.h>
#include<iostream>
using namespace std;
void kanchuan(char tu[][15][15],int m,int ti[][15][15],int n)
{
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
{
if(tu[m][i][j] == '.' && m == 0) for(int k = 0;k < n;k++) ti[j][k][n - i - 1] = 0;
if(tu[m][i][j] == '.' && m == 1) for(int k = 0;k < n;k++) ti[k][j][n - i - 1] = 0;
if(tu[m][i][j] == '.' && m == 2) for(int k = 0;k < n;k++) ti[n - j - 1][k][n - i - 1] = 0;
if(tu[m][i][j] == '.' && m == 3) for(int k = 0;k < n;k++) ti[k][n - j - 1][n - i - 1] = 0;
if(tu[m][i][j] == '.' && m == 4) for(int k = 0;k < n;k++) ti[j][i][k] = 0;
if(tu[m][i][j] == '.' && m == 5) for(int k = 0;k < n;k++) ti[j][n - i - 1][k] = 0;
}
}
int main()
{
int n;
while(cin>>n && n)
{
char tu[6][15][15];
int ti[15][15][15],ans = 0;
for(int i = 0; i < n; i++)
{
scanf("%s",tu[0][i]);
scanf("%s",tu[1][i]);
scanf("%s",tu[2][i]);
scanf("%s",tu[3][i]);
scanf("%s",tu[4][i]);
scanf("%s",tu[5][i]);
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++) ti[i][j][k] = 1;
kanchuan(tu,0,ti,n); //前视图
kanchuan(tu,1,ti,n); //左视图
kanchuan(tu,2,ti,n); //后视图
kanchuan(tu,3,ti,n); //右视图
kanchuan(tu,4,ti,n); //顶视图
kanchuan(tu,5,ti,n); //底视图
while(1)
{
int flag = 0; //0则正确
for(int i = 0; i < n; i++) //检测某个面是否应该被删除
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++)
if(ti[i][j][k])
{
char color = '0';
//检测可以对比扫描的面
int up = 1,down = 1,left = 1,right = 1,near = 1,notnear = 1; //1是未被挡住
for(int l = 0; l < i; l++) if(ti[l][j][k]) left = 0;
for(int l = i + 1; l < n; l++) if(ti[l][j][k]) right = 0;
for(int l = 0; l < j; l++) if(ti[i][l][k]) notnear = 0;
for(int l = j + 1; l < n; l++) if(ti[i][l][k]) near = 0;
for(int l = 0; l < k; l++) if(ti[i][j][l]) down = 0;
for(int l = k + 1; l < n; l++) if(ti[i][j][l]) up = 0;
//在各个视图中找到这个小方块,并对比
if(up && color == '0') color = tu[4][j][i];
if(down && color == '0') color = tu[5][n - j - 1][i];
if(left && color == '0') color = tu[1][n - k - 1][j];
if(right && color == '0') color = tu[3][n - k - 1][n - j - 1];
if(near && color == '0') color = tu[0][n - k - 1][i];
if(notnear && color == '0') color = tu[2][n - k - 1][n - i - 1];
if(up && color !=tu[4][j][i])
{
flag = 1;
ti[i][j][k] = 0;
continue;
}
if(down && color !=tu[5][n - j - 1][i])
{
flag = 1;
ti[i][j][k] = 0;
continue;
}
if(left && color != tu[1][n - k - 1][j])
{
flag = 1;
ti[i][j][k] = 0;
continue;
}
if(right && color !=tu[3][n - k - 1][n - j - 1])
{
flag = 1;
ti[i][j][k] = 0;
continue;
}
if(near && color !=tu[0][n - k - 1][i])
{
flag = 1;
ti[i][j][k] = 0;
continue;
}
if(notnear && color !=tu[2][n - k - 1][n - i - 1])
{
flag = 1;
ti[i][j][k] = 0;
continue;
}
}
if(!flag) break;
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
for(int k = 0; k < n; k++) if(ti[i][j][k]) ans++;
printf("Maximum weight: %d gram(s)\n",ans);
}
return 0;
}