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 <= 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.
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)
题意: 给你前左后右顶底6个面的颜色, 判断物体最大重量, n*n*n的正方体, 1*1*1的重量为1.
‘.’代表看透, 其余颜色A~Z表示
解题思路:
1. 题目不难, 可以看透的位置'.'表示这一方向上没有单位正方体,
减去.
2. 不同视角上如果同一位置颜色不一样表示这个方向不只一个单位正方体.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 12
int n;
char g[MAX][MAX][MAX];
char pos[MAX][MAX][MAX];
inline void getPos(int k, int i, int j, int p, int &x, int &y, int &z)
{
if(k == 0) { x = p, y = j, z = i; }
else if(k == 1) { x = n-1-j, y = p, z = i; }
else if(k == 2) { x = n-1-p, y = n-1-j, z = i; }
else if(k == 3) { x = j, y = n-1-p, z = i; }
else if(k == 4) { x = n-1-i, y = j, z = p; }
else if(k == 5) { x = i, y = j, z = n-1-p; }
}
int main()
{
//
freopen("input.txt", "r", stdin);
while(scanf("%d", &n) != EOF)
{
if(n == 0) break;
int x, y, z;
for(int i = 0; i < n; ++i)
for(int k = 0; k < 6; ++k)
for(int j = 0; j < n; ++j)
cin >> g[k][i][j];
for(int k = 0; k < n; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
pos[k][i][j] = '!';
for(int k = 0; k < 6; ++k)
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(g[k][i][j] == '.')
{
for(int p = 0; p < n; ++p)
{
getPos(k, i, j, p, x, y, z);
pos[x][y][z] = '.';
}
}
while(true)
{
bool flag = false;
for(int k = 0; k < 6; ++k)
{
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < n; ++j)
{
if(g[k][i][j] != '.')
{
for(int p = 0; p < n; ++p)
{
getPos(k, i, j, p, x, y, z);
if(pos[x][y][z] == '.') continue;
if(pos[x][y][z] == '!')
{
pos[x][y][z] = g[k][i][j];
break;
}
if(pos[x][y][z] == g[k][i][j]) break;
pos[x][y][z] = '.';
flag = true;
}
}
}
}
}
if( !flag ) break;
}
int result = 0;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
if(pos[i][j][k] != '.') result++;
printf("Maximum weight: %d gram(s)\n", result);
}
return 0;
}