ZOJ 2714 Uva LA 2995 Image is everything 机智题
题意:
一个n*n*n的立方体,每个单位格子可能是空的,或是一个纯色方块,给你六个面的视图,能看穿用.表示,否则是一个大写字母代表颜色。问最大可能的体积。
分析:
Final题,如同题目,考察机智程度的题。反正我是不够机智。。想半天不会,看了题解还是不知道怎么写。。最后参考了白书训练指南的代码,做法如下:
建一个3维数组存放整个立方体,首先对于能看穿的,那一溜过去都是空的。接下来就是一个染色判断矛盾的过程,循环所有的面的每个位置去给立方体对应位置染色,如果出现矛盾,这个位置就是空的,一直重复做到没有删除。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 char read(){ 7 char ret; 8 while(1){ 9 ret = getchar(); 10 if (ret == '.' || (ret >= 'A' && ret <= 'Z')) return ret; 11 } 12 } 13 int n; 14 void getp(int id, int r, int c, int d, int &x, int &y, int &z){ 15 if (id == 0) x = n-1-d, y = c, z = n-1-r; 16 if (id == 1) x = c, y = d, z = n-1-r; 17 if (id == 2) x = d, y = n-1-c, z = n-1-r; 18 if (id == 3) x = n-1-c, y = n-1-d, z = n-1-r; 19 if (id == 4) x = r, y = c, z = n-1-d; 20 if (id == 5) x = n-1-r, y = c, z = d; 21 } 22 int x, y, z; 23 char pos[15][15][15], view[6][15][15]; 24 int main() 25 { 26 while(scanf("%d", &n) && n) 27 { 28 for (int j = 0; j < n; j++) 29 for (int i = 0; i < 6; i++) 30 for (int k = 0; k < n; k++) 31 view[i][j][k] = read(); 32 for (int i = 0; i < n; i++) 33 for (int j = 0; j < n; j++) 34 for (int k = 0; k < n; k++) 35 pos[i][j][k] = '#'; 36 for (int i = 0; i < 6; i++) 37 for (int j = 0; j < n; j++) 38 for (int k = 0; k < n; k++) if (view[i][j][k] == '.'){ 39 for (int p = 0; p < n; p++){ 40 getp(i, j, k, p, x, y, z); 41 pos[x][y][z] = '.'; 42 } 43 } 44 bool flag = true; 45 while(flag){ 46 flag = false; 47 for (int i = 0; i < 6; i++) 48 for (int j = 0; j < n; j++) 49 for (int k = 0; k < n; k++) if (view[i][j][k] != '.'){ 50 for (int p = 0; p < n; p++){ 51 getp(i, j, k, p, x, y, z); 52 if (pos[x][y][z] == '.') continue; 53 if (pos[x][y][z] == '#'){ 54 pos[x][y][z] = view[i][j][k]; 55 break; 56 } 57 if (pos[x][y][z] == view[i][j][k]) break; 58 pos[x][y][z] = '.'; 59 flag = true; 60 } 61 } 62 } 63 int ans = 0; 64 for (int i = 0; i < n; i++) 65 for (int j = 0; j < n; j++) 66 for (int k = 0; k < n; k++) 67 if (pos[i][j][k] != '.') ans++; 68 printf("Maximum weight: %d gram(s)\n", ans); 69 } 70 return 0; 71 }