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 theobject from the 6 cardinal directions, and then inferring an upper limit on the object’s weight basedon 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 casebegins 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)
The problem is giving you six views of a n*n object. The object is formed by n*n unit blocks. It is easy to know that if we want to locate one block, we need to know the view number(which view), i, j, len(The deepth of the block). We can use these four parameters to know the (x,y,z) of the needed block. And what we need to do is to cut blocks from the object. But which block should be cut? First, if the MAPij in view k is ‘.’, that means there are no blocks in this direction, we need to cut (k(view number), i, j, len(from 1 to n)) blocks. Besides this, we need to judge if the situation is valid. If not, we need to find the invalid block, and cut it. What block is invalid? Every block is painted one color. So, if there is a block in (x,y,z), but the view shows that the block is painted by two or more kinds of color, then, the block is invalid, we have to cut it and judge again.
code :
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 12;
char view[maxn][maxn][maxn];
char map[maxn][maxn][maxn];
int n;
void get(int k,int len,int i,int j,int& x,int& y,int& z)
{// We can get (x, y, z) by using k, i, j, len
if(k == 1){x = j;y = len;z = n - i + 1;}
else if(k == 2){x = len;y = n - j + 1; z = n - i + 1;}
else if(k == 3){x = n - j +1;y = n - len + 1;z = n - i + 1;}
else if(k == 4){x = n - len + 1;y = j;z = n - i+1;}
else if(k == 5){x = j;y = n - i + 1;z = n - len + 1;}
else if(k == 6){x = j;y = i; z = len;}
}
int main()
{
while(scanf("%d",&n) && n)
{
int x,y,z;
for(int j = 1;j <= n;++ j)
{
for(int i = 1;i <= 6;++ i)
{
scanf("%s",view[i][j]+1);
}
}
memset(map,'#',sizeof(map));
for(int k = 1;k <= 6;++ k)
{
for(int i = 1;i <= n; ++ i)
{
for(int j = 1;j <= n;++ j)
{
if(view[k][i][j] == '.')
{
for(int p = 1;p <= n;++ p)
{
get(k,p,i,j,x,y,z);
map[x][y][z] = '.';
}
}
}
}
}
while(1)
{
bool ok = true;
for(int k = 1;k <= 6;++ k)
{
for(int i = 1;i <= n;++ i)
{
for(int j = 1;j <= n;++ j)
{
if(view[k][i][j] == '.')continue;
for(int p = 1;p <= n;++ p)
{
get(k,p,i,j,x,y,z);
if(map[x][y][z] == '.')continue;
if(map[x][y][z] == '#')
{
map[x][y][z] = view[k][i][j];
break;
}
if(map[x][y][z] == view[k][i][j])break;
map[x][y][z] = '.';
ok = false;
}
}
}
}
if(ok)break;
}
int ans = 0;
for(int i = 1;i <= n;++ i)
{
for(int j = 1;j <= n;++ j)
{
for(int k = 1;k <= n;++ k)
{
if(map[i][j][k] != '.')ans++;
}
}
}
printf("Maximum weight: %d gram(s)\n",ans);
}
return 0;
}