UVALive - 2995 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)

思路:开一个实体立方体,用六个面一个个来去除实体立方体的单位,最后留下的立方体就是答案。

白书代码:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
const int maxn=10;
int n;
char pos[maxn][maxn][maxn];
char view[maxn][maxn][maxn];
char read_char()
{
    char ch;
    while(1)
    {
        ch=getchar();
        if((ch>='A'&&ch<='Z')||ch=='.')
        {
            return ch;
        }
    }
}
void get(int k,int i,int j,int p,int &c,int &x,int &y)
{
if(k==0) {c=p;x=j;y=i;};//前
if(k==1){c=n-1-j;x=p;y=i;};//左
if(k==2){c=n-1-p;x=n-1-j;y=i;};//后
if(k==3){c=j;x=n-1-p;y=i;};//右
if(k==4){c=n-1-i;x=j;y=p;};//顶
if(k==5){c=i;x=j;y=n-1-p;};//底
}
int main()
{
    while(~scanf("%d",&n),n)
    {
        REP(i,n) REP(k,6) REP(j,n) view[k][i][j]=read_char();
        REP(i,n) REP(j,n) REP(k,n) pos[i][j][k]='#';
        REP(k,6) REP(i,n) REP(j,n) if(view[k][i][j]=='.')
        {
            REP(p,n)
            {
                int c,x,y;
                get(k,i,j,p,c,x,y);
                pos[c][x][y]='.';
            }
        }
        while(1)
        {
            int flag=true;
            REP(k,6) REP(i,n) REP(j,n) if(view[k][i][j]!='.')
            {
                REP(p,n)
                {
                    int c,x,y;
                    get(k,i,j,p,c,x,y);
                    if(pos[c][x][y]=='.') continue;
                    if(pos[c][x][y]=='#')
                    {
                        pos[c][x][y]=view[k][i][j];
                        break;
                    }
                    if(pos[c][x][y]==view[k][i][j]) break;
                    pos[c][x][y]='.';
                    flag=false;
                }
            }
            if(flag)break;
        }

        int ans=0;
        REP(k,n) REP(i,n) REP(j,n)
        if(pos[k][i][j]!='.')
            ans++;
        printf("Maximum weight: %d gram(s)\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41380961/article/details/81142716