题目链接:https://vjudge.net/problem/UVALive-2995
这道题的主要难点在于三维坐标系的建立,然后在坐标系中进行迭代更新。
注意用宏定义来简化代码。
AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++) using namespace std; const int maxn=;
int n;
char pos[maxn][maxn][maxn];
char view[][maxn][maxn]; inline char read_char(){
char c;
for(;;){
c=getchar();
if((c>='A'&&c<='Z')||c=='.') return c;
}
} inline void get(int k,int i,int j,int len,int &x,int &y,int &z){
if(k==){x=len;y=j;z=i;}
if(k==){x=n--j;y=len;z=i;}
if(k==){x=n--len;y=n--j;z=i;}
if(k==){x=j;y=n--len;z=i;}
if(k==){x=n--i;y=j;z=len;}
if(k==){x=i;y=j;z=n--len;}
} int main(){
while(scanf("%d",&n)==&&n){
rep(i,n) rep(k,) rep(j,n) view[k][i][j]=read_char();
rep(i,n) rep(j,n) rep(k,n) pos[i][j][k]='#';
rep(k,) rep(i,n) rep(j,n) if(view[k][i][j]=='.')
rep(p,n){
int x,y,z;
get(k,i,j,p,x,y,z);
pos[x][y][z]='.';
}
for(;;){
bool done=;
rep(k,) rep(i,n) rep(j,n) if(view[k][i][j]!='.'){
rep(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=;
}
}
if(done) break;
}
int ans=;
rep(i,n) rep(j,n) rep(k,n)
if(pos[i][j][k]!='.') ans++;
printf("Maximum weight: %d gram(s)\n",ans);
}
return ;
}
AC代码