程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> uva 1030 Image Is Everything(迭代更新)

uva 1030 Image Is Everything(迭代更新)

編輯:關於C++

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. Each 1×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 containing N, which is the size of the object ( 1$ \le$N$ \le$10). 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
#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;
}




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved