首页 > 代码库 > uva 1030 Image Is Everything(迭代更新)
uva 1030 Image Is Everything(迭代更新)
uva 1030 Image Is Everything
Your new company is building a robot that can hold small lightweight objects. The robot will have the intelligence to determine if an object is light enough to hold. It does this by taking pictures of the object from the 6 cardinal directions, and then inferring an upper limit on the object‘s weight based on those images. You must write a program to do that for the robot.
You can assume that each object is formed from an N×N×N lattice of cubes, some of which may be missing. Each1×1×1 cube weighs 1 gram, and each cube is painted a single solid color. The object is not necessarily connected.
Input
The input for this problem consists of several test cases representing different objects. Every case begins with a line containingN, which is the size of the object ( 1N10). The next N lines are the different N×N views of the object, in the order front, left, back, right, top, bottom. Each view will be separated by a single space from the view that follows it. The bottom edge of the top view corresponds to the top edge of the front view. Similarly, the top edge of the bottom view corresponds to the bottom edge of the front view. In each view, colors are represented by single, unique capital letters, while a period (.) indicates that the object can be seen through at that location.Input for the last test case is followed by a line consisting of the number 0.
Output
For each test case, print a line containing the maximum possible weight of the object, using the format shown below.Sample Input
3 .R. YYR .Y. RYY .Y. .R. GRB YGR BYG RBY GYB GRB .R. YRR .Y. RRY .R. .Y. 2 ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ ZZ 0
Sample Output
Maximum weight: 11 gram(s) Maximum weight: 8 gram(s)
题目大意:有一个最大为n*n*n的立方体的一个不规整立体,由若干个1*1*1的小正方体构成(每一个小正方体被涂成不同的颜色),给出n,然后是该立体的前、左、后、右、上和下的视图,然后判断该立体的最大体积是多少。
#include<stdio.h> #include<string.h> #define REP(i,n) for (int i = 0; i < (n); i++) //宏定义循环,简化代码 int n; char img[15][15][15], pos[15][15][15]; char getch() { char ch; while (true) { ch = getchar(); if ((ch >= 'A' && ch <= 'Z') || ch == '.') return ch; } } void get(int i, int j, int k, int m, int& x, int& y, int& z) { //在第k个视图中,i行j列深度为m的单位立方体,在原立方体中的坐标(x,y,z) switch(k) { case 0: x = i, y = j, z = m; return; case 1: x = i, y = m, z = n - j - 1; return; case 2: x = i, y = n - j - 1, z = n - m - 1; return; case 3: x = i, y = n - m - 1, z = j; return; case 4: x = m, y = j, z = n - i - 1; return; case 5: x = n - m - 1, y = j, z = i; return; } } int main() { while (scanf("%d\n", &n) == 1, n) { REP(i, n) REP(k, 6) REP(j, n) img[k][i][j] = getch(); REP(x, n) REP(y, n) REP(z, n) pos[x][y][z] = '*'; int x, y, z; REP(k, 6) REP(i, n) REP(j, n) //能“看穿”的位置,单位方块一定都不存在 if (img[k][i][j] == '.') { REP(m, n) { get(i, j, k, m, x, y, z); pos[x][y][z] = '.'; } } while (true) { int flag = 1; REP(k, 6) REP(i, n) REP(j, n) if (img[k][i][j] != '.') { REP(p, n) { get(i, j, k, p, x, y, z); if (pos[x][y][z] == '.') continue; if (pos[x][y][z] == '*') { pos[x][y][z] = img[k][i][j]; //给方块“上色” } if (pos[x][y][z] == img[k][i][j]) break; pos[x][y][z] = '.'; //若一个单位方块有不同的颜色,则该方块不存在 flag = 0; } } if (flag) break; } int sum = 0; REP(x, n) REP(y, n) REP(z, n) //统计单位方块数 if (pos[x][y][z] != '.') sum++; printf("Maximum weight: %d gram(s)\n", sum); } return 0; }
uva 1030 Image Is Everything(迭代更新)