问题
分析
这题主要是思路得清晰,然后想法很直接,就是模拟,然后求解,没有什么弯路,但是很容易在细节上出错,锻炼使用宏REP代替for循环,精简程序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define REP(i,n) for(int i=0;i<(n);++i)
using namespace std;
const int maxn=10;
int n;
char pos[maxn][maxn][maxn],view[6][maxn][maxn];
char readChar(){
char c;
while(true){
c=getchar();
if(isalpha(c) || c=='.')
return c;
}
}
//第k个视图中i行,j列,dep深度的格子对应的x,y,z坐标
void getPos(int k,int i,int j,int dep,int &x,int &y,int &z){
if(k==0) {x=dep; y=j; z=i;}
else if(k==1) {x=n-1-j; y=dep; z=i;}
else if(k==2) {x=n-1-dep; y=n-1-j; z=i;}
else if(k==3) {x=j; y=n-1-dep; z=i;}
else if(k==4) {x=n-1-i; y=j; z=dep;}
else if(k==5) {x=i; y=j; z=n-1-dep;}
}
int main(void){
while(scanf("%d",&n)==1 && n) {
REP(i,n) REP(k,6) REP(j,n) view[k][i][j]=readChar();
//一开始正方体是满的
REP(i,n) REP(j,n) REP(k,n) pos[i][j][k]='#';
//开始删除'.'对应的格子
int x,y,z;
REP(k,6) REP(i,n) REP(j,n) if(view[k][i][j]=='.'){
REP(len,n){
getPos(k,i,j,len,x,y,z);
pos[x][y][z]='.';
}
}
//删除互相矛盾的格子
while(true){
bool ok=true;
REP(k,6) REP(i,n) REP(j,n){
if(view[k][i][j]!='.'){
REP(len,n){
getPos(k,i,j,len,x,y,z);
if(pos[x][y][z]=='.') continue;
else if(pos[x][y][z]=='#' ) {
pos[x][y][z] = view[k][i][j];
break;
}else if(pos[x][y][z]==view[k][i][j]) break;
else if(pos[x][y][z]!=view[k][i][j]){
ok=false;
pos[x][y][z]='.';
}
}
}
}
if(ok) break;
}
int ans=0;
REP(i,n) REP(j,n) REP(k,n) if(pos[i][j][k]!='.') ++ans;
printf("Maximum weight: %d gram(s)\n",ans);
}
}