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". 
POJ 1052 Plato's Blocks

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 }
View Code

 

你可能感兴趣的:(block)