1052:Plato‘s Blocks(柏拉图的积木)(已翻译)

描述

Plato believed what we perceive is but a shadow of reality. Recent archaeological excavations have uncovered evidence that this belief may have been encouraged by Plato's youthful amusement with cleverly-designed blocks. The blocks have the curious property that, when held with any face toward a light source, they cast shadows of various letters, numbers, shapes, and patterns. It is possible for three faces incident to a corner to correspond to three different shadow patterns. Opposite faces, of course, cast shadows which are mirror images of one another.
The blocks are formed by gluing together small cubes to form a single, connected object. As an example, the figures below show, layer by layer, the internal structure of a block which can cast shadows of the letters "E", "G", or "B".

Only a partial set of blocks was discovered, but the curious scientists would like to determine what combinations of shadows are possible. Your program, the solution to this problem, will help them! The program will input groups of shadow patterns, and for each group will report whether or not a solid can be constructed that will cast those three shadows.

柏拉图认为我们所感知到的不过是现实的影子。最近的考古发掘已经发现证据表明,这种信仰可能是受到了柏拉图年轻时对巧妙设计的积木的乐趣的鼓励。这些积木有一种奇特的特性,当任何面朝光源的时候,它们会投射出各种字母、数字、形状和图案的阴影。有可能三个面对一个角落事件对应三种不同的阴影模式。当然,相反的面孔投下的影子是彼此的镜像。这些块是通过将小立方体粘合在一起形成一个单独的、连接的对象。例如,下面的图片一层一层地显示了一个块的内部结构,它可以投射字母“ E”、“ G”或“ B”的阴影。虽然只发现了部分块,但好奇的科学家们想要确定阴影的组合是可能的。写出这个问题的解决方案,将帮助他们!该程序将输入阴影模式组,并为每组将报告是否可以建立一个立体,将投射这三个阴影。

输入
The input contains a sequence of data sets, each specifying a dimension and three shadow patterns. The first line of a data set contains a positive integer 1 <= n <= 20 that specifies the dimensions of the input patterns. The remainder of the data set consists of 3n lines, each containing a string of n "X" and "-" characters. Each group of n lines represents a pattern. Where an "X" appears a shadow should be cast by the final solid, and where a "-" appears, light should pass through. For this problem, the input patterns may be assumed to have at least one "X" along each edge of the pattern. The input is terminated by a line containing a single zero in place of a valid dimension.

输入包含一系列数据集,每个数据集指定一个维度和三个阴影模式。数据集的第一行包含一个正整数1 < = n < = 20,它指定输入模式的维度。数据集的其余部分由3n 行组成,每行包含 n 个“ X”和“-”字符的字符串。每组 n 行代表一个模式。当“ X”出现时,阴影应该由最终的实体投射,而当“-”出现时,光线应该通过。对于这个问题,可以假定输入模式在模式的每条边上至少有一个“ X”。输入由一行结束,该行包含一个零代替有效的维度。

输出
For each data set in the input, output the data set number and one of the following messages:

Valid set of patterns
Impossible combination
For a set of patterns to be considered valid, it must be possible to construct, by gluing unit cubes together along their faces, a one-piece solid capable of casting the shadow of each of the input patterns.

对于输入中的每个数据集,输出数据集编号和以下信息之一: 有效的模式集合不可能的组合对于一组被认为是有效的模式,必须有可能通过将单位立方体沿着它们的表面粘合在一起来构造一个能够投射每个输入模式阴影的整体。

样例输入
5
XXXXX
X----
X--XX
X---X
XXXXX
XXXXX
X----
XXXXX
X----
XXXXX
XXXXX
X---X
XXXX-
X---X
XXXXX
3
X--
-X-
--X
XX-
XXX
-XX
-XX
XXX
XX-
0
样例输出
Data set 1: Valid set of patterns
Data set 2: Impossible combination
来源
Greater New York 2001


分析

把给出的三个面当三视图,判断它能否组成一个物体。坑点在于给出的每个面都有8种可能情况:旋转,翻转,翻转之后再旋转。


代码
#include <stdio.h>
 
bool a[20][20][20],vis[20][20][20];
char b[8][20][21],c[8][20][21],d[8][20][21];
int n;
 
void search(int x,int y,int z)
{
    vis[x][y][z]=1;
    if(x+1<n && !vis[x+1][y][z] && a[x+1][y][z]) search(x+1,y,z);
    if(y+1<n && !vis[x][y+1][z] && a[x][y+1][z]) search(x,y+1,z);
    if(z+1<n && !vis[x][y][z+1] && a[x][y][z+1]) search(x,y,z+1);
    if(x-1>=0 && !vis[x-1][y][z] && a[x-1][y][z]) search(x-1,y,z);
    if(y-1>=0 && !vis[x][y-1][z] && a[x][y-1][z]) search(x,y-1,z);
    if(z-1>=0 && !vis[x][y][z-1] && a[x][y][z-1]) search(x,y,z-1);
}
 
int main()
{
    int i,j,k,flag,ok,cnt=1,count,o,p,q;
 
    while(scanf("%d",&n) && n)
    {
        for(i=0;i<n;i++) scanf("%s",b[0][i]);
        for(i=0;i<n;i++) scanf("%s",c[0][i]);
        for(i=0;i<n;i++) scanf("%s",d[0][i]);
 
        for(k=1;k<=3;k++)//考虑各个面的不同情况
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    b[k][i][j]=b[k-1][j][n-1-i];
                    c[k][i][j]=c[k-1][j][n-1-i];
                    d[k][i][j]=d[k-1][j][n-1-i];
                }
            }
        }
        for(k=4;k<=4;k++)//考虑各个面的不同情况
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    b[4][i][j]=b[0][n-1-i][n-1-j];
                    c[4][i][j]=c[0][n-1-i][n-1-j];
                    d[4][i][j]=d[0][n-1-i][n-1-j];
                }
            }
        }
        for(k=5;k<=7;k++)//考虑各个面的不同情况
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    b[k][i][j]=b[k-1][j][n-1-i];
                    c[k][i][j]=c[k-1][j][n-1-i];
                    d[k][i][j]=d[k-1][j][n-1-i];
                }
            }
        }
 
        ok=0;
        for(o=0;o<8 && !ok;o++) for(p=0;p<8 && !ok;p++) for(q=0;q<8 && !ok;q++)
        {
            for(i=0;i<n;i++) for(j=0;j<n;j++) for(k=0;k<n;k++) a[i][j][k]=vis[i][j][k]=0;
 
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    for(k=0;k<n;k++)
                    {
                        if(b[o][i][j]=='X' && c[p][j][k]=='X' && d[q][i][k]=='X') a[i][j][k]=1;
                    }
                }
            }
 
            ok=1;
 
            for(i=0;i<n && ok;i++)
            {
                for(j=0;j<n && ok;j++)
                {
                    if(b[o][i][j]=='X')
                    {
                        flag=0;
 
                        for(k=0;k<n && ok;k++)
                        {
                            if(a[i][j][k])
                            {
                                flag=1;
                                break;
                            }
                        }
 
                        if(!flag)
                        {
                            ok=0;
                            break;
                        }
                    }
                }
            }
 
            for(j=0;j<n && ok;j++)
            {
                for(k=0;k<n && ok;k++)
                {
                    if(c[p][j][k]=='X')
                    {
                        flag=0;
 
                        for(i=0;i<n && ok;i++)
                        {
                            if(a[i][j][k])
                            {
                                flag=1;
                                break;
                            }
                        }
 
                        if(!flag)
                        {
                            ok=0;
                            break;
                        }
                    }
                }
            }
 
            for(i=0;i<n && ok;i++)
            {
                for(k=0;k<n && ok;k++)
                {
                    if(d[q][i][k]=='X')
                    {
                        flag=0;
 
                        for(j=0;j<n && ok;j++)
                        {
                            if(a[i][j][k])
                            {
                                flag=1;
                                break;
                            }
                        }
 
                        if(!flag)
                        {
                            ok=0;
                            break;
                        }
                    }
                }
            }
 
            count=0;
 
            for(i=0;i<n && ok;i++)
            {
                for(j=0;j<n && ok;j++)
                {
                    for(k=0;k<n && ok;k++)
                    {
                        if(a[i][j][k] && !vis[i][j][k])
                        {
                            count++;
                            search(i,j,k);
                        }
                    }
                }
            }
 
            if(count!=1) ok=0;
 
            if(ok)
            {
                printf("Data set %d: Valid set of patterns\n",cnt++);
                break;
            }
 
        }
 
        if(!ok) printf("Data set %d: Impossible combination\n",cnt++);
    }
}

给个赞和关注吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值