立方体成像(2004年ACM国际大学生程序设计决赛第3题)

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 = N =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



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


Maxinum weight 11 gram(s)
Maxinum weight 8 gram(s)


题意:

有一个 n * n * n 的立方体,其中一些单位立方体已经缺失(剩下部分不一定连通)。每个单位立方体重 1 克,且被涂上单一的颜色(即 6 个面的一颜色相同)。给出前、左、后、右、顶、底 6 个视图,你的任务是判断这个屋里剩下的最大的重量。

挺考空间想象力和逻辑能力的,有思路没思路最好都先思考下,看能想到哪一步。

#include 
using namespace std;

#define MAXN 10
#define FOR(i, n) for (i = 0; i < (n); i++)
int n;
int i, j, k, p;
int view[6][MAXN][MAXN];   //视图
int cube[MAXN][MAXN][MAXN];//立方体


char read_char()
{
	char ch;
	for (;;)
	{
		ch = getchar();
		if ((ch >= 'A' && ch <= 'Z') || ch == '.') return ch;
	}
}

/*视图与立方体坐标的对应关系*/
void getXYZ(int k, int i, int j, int len, int &x, int &y, int &z)
{
	if (k == 0) { x = len; y = j; z = i;}
	if (k == 1) { x = n - 1 - j; y = len; z = i;}
	if (k == 2) { x = n - 1 - len; y = n - 1 -j; z = i;}
	if (k == 3) { x = j; y = n - 1 - len; z = i;}
	if (k == 4) { x = n - 1 - i; y = j; z = len;}
	if (k == 5) { x = i; y = j; z = n - 1 - len;}
}

int main()
{
	while (cin>>n, n)
	{
		FOR(i, n) FOR(k, 6) FOR(j, n) view[k][i][j] = read_char();//读入视图
		FOR(i, n) FOR(j, n) FOR(k, n) cube[i][j][k] = '#';		  //立方体初始化为全#

		/*处理'.'的情况*/
		FOR(k, 6) FOR(i, n) FOR(j, n) if (view[k][i][j] == '.')
		FOR(p, n)//发现一个'.'清空对应n层
		{
			int x, y, z;
			getXYZ(k, i, j, p, x, y, z);
			cube[x][y][z] = '.';
		}

		/*处理颜色情况*/
		for (;;)
		{
			bool done = true;
			FOR(k, 6) FOR(i, n) FOR(j, n) if (view[k][i][j] != '.')
			FOR(p, n)//深入视图n层
			{
				int x, y, z;
				getXYZ(k, i, j, p, x, y, z);
				if (cube[x][y][z] == '.') continue;       //不存在
				if (cube[x][y][z] == '#')			      //没标记过
				{
					cube[x][y][z] = view[k][i][j];
					break;
				}
				if (cube[x][y][z] ==  view[k][i][j]) break;//颜色相同
				/*其余的就是不同色,删之*/
				cube[x][y][z] = '.';
				done = false;
			}
			if (done) break;
		}

		int ans = 0;
		FOR(i, n) FOR(j, n) FOR(k, n) if (cube[i][j][k] != '.') ans ++;
		printf("Maxinum weight %d gram(s)\n", ans);
	}
	return 0;
}



你可能感兴趣的:(历年国际ACM比赛)