POJ 1052 Plato's Blocks

Plato‘s Blocks

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 734   Accepted: 296

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

Data set 1: Valid set of patterns
Data set 2: Impossible combination

这个问题刚开始没有思路,看了网上的一些方法,就自己写了一个;

  1 //注意每个面都由八种方式,旋转+翻转
  2 //先建一个完整的立方块,然后删去中间的空缺部分
  3 //最后检查一下(深搜)是不是所有的小立方块都连在一起
  4
  5 #include<iostream>
  6 #include<algorithm>
  7 #include<cstring>
  8 #include<cstdio>
  9
 10 using namespace std;
 11 char mpr[20][20];
 12 int n;
 13 char mp[3][8][20][20];
 14 char cube[20][20][20];
 15 int dx[]={0,0,0,0,1,-1};
 16 int dy[]={0,0,1,-1,0,0};
 17 int dz[]={1,-1,0,0,0,0};
 18 void cs(int t)
 19 {
 20     for(int i=0;i<n;i++)
 21         for(int j=0;j<n;j++)
 22             mp[t][0][i][j]=mpr[i][j];
 23     for(int i=0;i<n;i++)
 24         for(int j=0;j<n;j++)
 25             mp[t][1][i][j]=mp[t][0][i][n-1-j];
 26     for(int i=0;i<n;i++)
 27         for(int j=0;j<n;j++)
 28             mp[t][2][i][j]=mp[t][1][j][n-1-i];
 29     for(int i=0;i<n;i++)
 30         for(int j=0;j<n;j++)
 31             mp[t][3][i][j]=mp[t][0][i][n-1-j];
 32     for(int i=0;i<n;i++)
 33         for(int j=0;j<n;j++)
 34             mp[t][4][i][j]=mp[t][3][j][n-1-i];
 35     for(int i=0;i<n;i++)
 36         for(int j=0;j<n;j++)
 37             mp[t][5][i][j]=mp[t][4][i][n-1-j];
 38     for(int i=0;i<n;i++)
 39         for(int j=0;j<n;j++)
 40             mp[t][6][i][j]=mp[t][5][j][n-1-i];
 41     for(int i=0;i<n;i++)
 42         for(int j=0;j<n;j++)
 43             mp[t][7][i][j]=mp[t][6][i][n-1-j];
 44 }
 45 int checkview1(int a)
 46 {
 47     for(int i=0;i<n;i++)
 48         for(int j=0;j<n;j++)
 49     {
 50         if(mp[0][a][i][j]==‘X‘)
 51         {
 52             int flag=0;
 53             for(int k=0;k<n;k++)
 54                 if(cube[i][j][k]==0)
 55                     flag=1;
 56             if(flag==0) return 0;
 57         }
 58     }
 59     return 1;
 60 }
 61 int checkview2(int a)
 62 {
 63     for(int i=0;i<n;i++)
 64         for(int j=0;j<n;j++)
 65     {
 66         if(mp[1][a][i][j]==‘X‘)
 67         {
 68             int flag=0;
 69             for(int k=0;k<n;k++)
 70                 if(cube[i][k][j]==0)
 71                     flag=1;
 72             if(flag==0) return 0;
 73         }
 74     }
 75     return 1;
 76 }
 77 int checkview3(int a)
 78 {
 79     for(int i=0;i<n;i++)
 80         for(int j=0;j<n;j++)
 81     {
 82         if(mp[2][a][i][j]==‘X‘)
 83         {
 84             int flag=0;
 85             for(int k=0;k<n;k++)
 86                 if(cube[k][i][j]==0)
 87                     flag=1;
 88             if(flag==0) return 0;
 89         }
 90     }
 91     return 1;
 92 }
 93 int check(int x,int y,int z)
 94 {
 95     if(x<n&&x>=0&&y<n&&y>=0&&z<n&&z>=0) return 1;
 96     return 0;
 97 }
 98 void dfs(int a,int b,int c)
 99 {
100     for(int i=0;i<6;i++)
101     {
102         int curx=a+dx[i];
103         int cury=b+dy[i];
104         int curz=c+dz[i];
105         if(check(curx,cury,curz)&&cube[curx][cury][curz]==0)
106         {
107             cube[curx][cury][curz]=1;
108             dfs(curx,cury,curz);
109         }
110     }
111     return ;
112 }
113 int Num()
114 {
115     int num=0;
116     for(int i=0;i<n;i++)
117         for(int j=0;j<n;j++)
118             for(int k=0;k<n;k++)
119                 if(cube[i][j][k]==0)
120         {
121             cube[i][j][k]=1;
122             dfs(i,j,k);
123             num++;
124         }
125     if(num>1) return 0;
126     return 1;
127 }
128 int solve(int a,int b,int c)
129 {
130     //建一个完全的立方块
131     memset(cube,0,sizeof(cube));
132
133     //删去其中的空缺部分
134     for(int i=0;i<n;i++)
135         for(int j=0;j<n;j++)
136             if(mp[0][a][i][j]==‘-‘)
137                 for(int k=0;k<n;k++)
138                     cube[i][j][k]=1;
139     for(int i=0;i<n;i++)
140         for(int j=0;j<n;j++)
141             if(mp[1][b][i][j]==‘-‘)
142                 for(int k=0;k<n;k++)
143                     cube[i][k][j]=1;
144     for(int i=0;i<n;i++)
145         for(int j=0;j<n;j++)
146             if(mp[2][c][i][j]==‘-‘)
147                 for(int k=0;k<n;k++)
148                     cube[k][i][j]=1;
149
150
151     //检查三视图是否还是符合的还有立方块是否都连在一起(dfs)
152     if(checkview1(a)&&checkview2(b)&&checkview3(c)&&Num())
153         return 1;
154     return 0;
155 }
156 int main()
157 {
158     int num=1;
159     while(cin >> n&&n)
160     {
161         for(int i=0;i<3;i++)
162         {
163             for(int j=0;j<n;j++)
164                 cin >> mpr[j];
165             cs(i);//这里构建八个面,那么8*8*8=512种情况,只要有一种情况符合就行了
166         }
167         int flag=0;
168         for(int i=0;i<8;i++)
169             for(int j=0;j<8;j++)
170                 for(int k=0;k<8;k++)
171                     if(solve(i,j,k))//检查每种情况是否符合
172                         flag=1;
173         if(flag)
174             printf("Data set %d: Valid set of patterns\n",num++);
175         else printf("Data set %d: Impossible combination\n",num++);
176     }
177     return 0;
178 }

POJ 1052 Plato's Blocks

时间: 2024-10-21 16:00:11

POJ 1052 Plato's Blocks的相关文章

[2016-03-11][POJ][1609][Tiling Up Blocks]

[2016-03-11][POJ][1609][Tiling Up Blocks] Tiling Up Blocks Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status Description Michael The Kid receives an interesting game set from his grandparent as his birthday gift. I

POJ 1609 Tiling Up Blocks.

~~~~ 二维的最长上升子序列.n^2算法居然可以水过.. 就不多说了,排个序,然后DP. 题目链接:http://poj.org/problem?id=1609 ~~~~ #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define N 11111 using namespace std; struct node { int l,m; }b[N]; boo

poj 1609 Tiling Up Blocks dp入门之记忆化搜索

题意: 给n个二元组(a,b),要在其中找最长的序列,使得对序列中的任意i<j,有ai<=aj且bi<=bj. 分析: 设dp[a][b]代表以(a,b)结尾的最长序列长度,记忆化搜索即可. 代码: //poj 1609 //sep9 #include <iostream> using namespace std; const int max_p=128; int n; int num[max_p][max_p]; int dp[max_p][max_p]; int sear

POJ 1052 MPI Maelstrom

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5547   Accepted: 3458 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

zoj题目分类

饮水思源---zoj 转载自:http://bbs.sjtu.edu.cn/bbscon,board,ACMICPC,file,M.1084159773.A.html 注:所有不是太难的题都被归成了“简单题”,等到发现的时候已经太晚了,我太死脑筋 了……:( 有些题的程序我找不到了,555……:( SRbGa的题虽然都很经典……但是由于其中的大部分都是我看了oibh上的解题报告后做 的,所以就不写了…… 题目排列顺序没有规律……:( 按照个人感觉,最短路有的算做了DP,有的算做了图论. 有些比较

POJ 3734 Blocks

                                                         POJ 3734   Blocks Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Panda has received an assignment of painting a line of blocks. Since Panda is such an

51nod 1052最大M子段和 &amp; poj 2479最大两子段和

最大子段和经典问题的扩展. 题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1052 N个整数组成的序列a[1],a[2],a[3],…,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M >= N个数中正数的个数,那么输出所有正数的和. 例如:-2 11 -4 13 -5 6 -2,分为2段,11 -4 13一段,6一段,和为26. Input 第1行:2个数N和M,中间用空格分

poj 3734 Blocks 【矩阵快速幂】

Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4529 Accepted: 2044 Description Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting.