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. Each 1×1×1 cube weighs 1 gram, and each cube is painted a single solid color. The object is not necessarily connected.
Input for the last test case is followed by a line consisting of the number 0.
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
Maximum weight: 11 gram(s) Maximum weight: 8 gram(s)
題目大意:有一個最大為n*n*n的立方體的一個不規整立體,由若干個1*1*1的小正方體構成(每一個小正方體被塗成不同的顏色),給出n,然後是該立體的前、左、後、右、上和下的視圖,然後判斷該立體的最大體積是多少。
#include#include #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; }