LA 2995 立方体成像(模拟)
题目链接:https://vjudge.net/problem/UVALive-2995
这道题的主要难点在于三维坐标系的建立,然后在坐标系中进行迭代更新。
注意用宏定义来简化代码。
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #define rep(i,n) for(int i=0;i<n;i++) 7 8 using namespace std; 9 10 const int maxn=10; 11 int n; 12 char pos[maxn][maxn][maxn]; 13 char view[6][maxn][maxn]; 14 15 inline char read_char(){ 16 char c; 17 for(;;){ 18 c=getchar(); 19 if((c>='A'&&c<='Z')||c=='.') return c; 20 } 21 } 22 23 inline void get(int k,int i,int j,int len,int &x,int &y,int &z){ 24 if(k==0){x=len;y=j;z=i;} 25 if(k==1){x=n-1-j;y=len;z=i;} 26 if(k==2){x=n-1-len;y=n-1-j;z=i;} 27 if(k==3){x=j;y=n-1-len;z=i;} 28 if(k==4){x=n-1-i;y=j;z=len;} 29 if(k==5){x=i;y=j;z=n-1-len;} 30 } 31 32 int main(){ 33 while(scanf("%d",&n)==1&&n){ 34 rep(i,n) rep(k,6) rep(j,n) view[k][i][j]=read_char(); 35 rep(i,n) rep(j,n) rep(k,n) pos[i][j][k]='#'; 36 rep(k,6) rep(i,n) rep(j,n) if(view[k][i][j]=='.') 37 rep(p,n){ 38 int x,y,z; 39 get(k,i,j,p,x,y,z); 40 pos[x][y][z]='.'; 41 } 42 for(;;){ 43 bool done=1; 44 rep(k,6) rep(i,n) rep(j,n) if(view[k][i][j]!='.'){ 45 rep(p,n){ 46 int x,y,z; 47 get(k,i,j,p,x,y,z); 48 if(pos[x][y][z]=='.') continue; 49 if(pos[x][y][z]=='#'){ 50 pos[x][y][z]=view[k][i][j]; 51 break; 52 } 53 if(pos[x][y][z]==view[k][i][j]) break; 54 pos[x][y][z]='.'; 55 done=0; 56 } 57 } 58 if(done) break; 59 } 60 int ans=0; 61 rep(i,n) rep(j,n) rep(k,n) 62 if(pos[i][j][k]!='.') ans++; 63 printf("Maximum weight: %d gram(s)\n",ans); 64 } 65 return 0; 66 }