uva 197 Cube

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=133

题目描述:

Cube 

There was once a 3 by 3 by 3 cube built of 27 smaller cubes. It has fallen apart into seven pieces:

Figure 1: The seven pieces that once formed a cube
\begin{figure}\begin{center}\mbox{}\epsfbox{p197a.eps}\end{center}\end{figure}

The seven pieces can be assembled in many ways to again form the cube. Figure 2 shows one of these possibilities. The first square stands for the front plane, the next one for the middle plane and the last one for the back plane of the cube. The letters in the cells stand for the name of piece filling out the corresponding space in the cube. The name of the seven pieces can be found in figure 1.

Figure 2: Two possibilities of assembling the cube
\begin{figure}\begin{center}\begin{tabular}{\vert ccc\vert c\vert ccc\vert c\ve......c \\\cline{1-3} \cline{5-7} \cline{9-11}\end{tabular}\end{center}\end{figure}

You are to write a program that outputs all possibilities of assembling the cube but suppress solutions that are mere rotations of another solution.


Hint: Piece a is the only part that, by rotation and translation, cannot be transformed into itself. In order to avoid solutions that are mere rotations of an already found solution, you may restrict transformations of piece a to translations.

Input 

The input file has several test cases. Each test case indicates the initial position of piece ` a '. You can translate it, but you mustn't rotate it.

Output 

For each solution found, your program should output a line containing the solution as a string. The string is a linearized form of the cube. Each letter stands for the piece filling out the corresponding space in the cube. It is linearized as follows:
  • The string consists of substrings representing the front, middle and back plane.
  • Each substring consists of substrings representing the top, middle and bottom row.
  • Each row substring consists of letters representing the left, middle and right cell.
The solutions in figure 2 would be represented like this:
adcaccaacddgbfgffedggbfebee
aababbadcffegfcddcfeeggedgc

It is very important that your program uses the naming convention given in figure 1 and linearizes the cube as explained above.

Print a blank line after each test case.

Figure 3: Positions of the cells in the string
\begin{figure}\begin{center}\mbox{}\epsfbox{p197b.eps}\end{center}\end{figure}

Figure 3 again shows how the cells of the cube are linearized.

Sample input 

aa.a..a....................
.........a..a..aa..........

Sample output 

aababbadcggeffcddcgeegfedfc
aababbadceffgdcgdceefedfggc
...
aababbadcffegfcddcfeeggedgc

adcaccaacfddfebgeeffdggbgeb
...

题意:有a~g七 种碎片,其中 要求给定a的输入后 a碎片就只能做平移变换(给出输入a的不同,实际上就是以不同方式旋转的a碎片然后通过一定的平移所得),b~g可以做平移和旋转变换,问通过不同的组合组成一个立体3*3*3的立方体有哪些不同的组合形式(不同的组合要全部输出来,仅仅是旋转角度不同的属于重复解,提示说明要利用a的不旋转属性来做组合的方式的不重复性)。

题解:这题看起来就是DFS不同的碎片凑成一个合格的立方体,但是中间加入了三维立体的元素 包括诸如旋转和平移,显得在求立体(立体碎片)的所有构形上很麻烦,后来看网上博客说要用矩阵变换,然后才有点眉目。(构形即是指物体通过旋转产生的不同的不重复的形状 或 摆放方式 或其他描述立体状态的词)

算法的大体流程:

1、首选我们要根据碎片,求出所有碎片的不重复的三维构形,可以推论得到碎片的最多不同构形有6*4=24种,其中6表示,无论是碎片还是立体都会有6个面,依次使6个面朝向自己,这样相当于有6种不同的旋转方式了(对于非对称立体是这样,对称的是不一定的),然后,在每个面朝向自己的构形状态下,都统一绕z/x/y(绕其中一个轴就行,轴的选定随意,但6个面朝向的构形都要统一绕此轴)轴绕0度,90度,180度,270度,这样就有4种不同构形(非对称),这样综合起来。对于对称 还是 非对称的立体构形最多有6*4=24种不同构形。这里非对称的a立体构形 刚好有24种旋转方式使其有24种不同构形,而对称的b~g的不同构形数量 是要小于24的。

2、经过这样的旋转变换后将得到的24种旋转构形,然后每一种构形还可以平移,平移理论上最多有3*3*3=27种,在每一种旋转上又产生相应的27种平移操作,得到24*27种不同构形状态,经过排序,去重(b~g产生的24中 是包含有重复构形的)等规格化处理,封装到一定的数据结构组织的库中,而这个库就是包含了a~g所有的旋转 平移 产生的不同的构形状态(a作特殊处理),这样就方便于后面的DFS 调用取舍这些构形状态。

3、得到a的一个输入后,将a不断平移(不能作旋转变换),然后以这样的a状态 作为DFS的初始化状态 进行 DFS,DFS过程中 深度为碎片的种类,一直从第2个碎片取到第7个碎片,若到最后立体构形能组成一个填满的立方体,则输出这一种组合方式。


算法dfs的伪码:

算法伪代码:

DFS(碎片)
{
    for(遍历同类型碎片库中能够放入当前立体的碎片构形)//每个类型最多有24*27种
        DFS(下一碎片)
}


基本的流程是这样,剩余的细节会在代码中讲到。

这样我们的纯DFS无剪枝的程序就出来了,并且实测能够过掉uva上的数据,并且也没有超过30s的限制:


代码1的说明部分 将会详述代码的细节部分


代码1写完后,介于运行时间实在是长,想到DFS应该剪枝,遂对DFS进行了改进,即增加了一条剪枝规则,不过我感觉剪枝不仅仅有这样1条,知道其他的同学欢迎补充。

代码2(DFS剪枝代码)中的剪枝策略:(唉,只有一条。。。)

1、在DFS过程中,我们很容易想到,如果目前的立体状态中(代码DFS中的string Solid),存在无论如何填补都不可填补的空缺块(‘.’),或者在DFS接下来类型的碎片中都不能填补这个不可填的空缺块,那么我们可以早早地回溯(或者说剪掉这条深搜路径)。(具体实现的说明见 代码2说明)


剪枝后的运行状况:(感觉运行时间还是很长)




代码1说明:由于代码过长,代码注释也不太能解释清楚,这里特在代码中提出几点特殊的地方解释。

说明一、关于如何生成24种所有构形,这里涉及到将一维坐标的27个点转换成3维坐标下的点,然后利用矩阵变换实现24种旋转方式,再将3维坐标转换回一维坐标27个点的形式。关于矩阵的变换的具体知识可以参考一下链接:

矩阵变换链接:

http://17de.com/library/d3d_6im/d3dim6_5.htm#2.3 旋转

http://wenku.baidu.com/view/4e813207e87101f69e3195c0.html

http://blog.csdn.net/sniffer_wang/article/details/6456183


我们主要会用到以下公式:

下面的变换将一个点(x, y, z)沿x-轴进行旋转,得到了一个新的点(x', y', z')

pic21.gif (1959 bytes)

防止图挂 文字写出:

[x' y' z' 1]=[x y z 1]A  (矩阵相乘)

矩阵A=

[1    0        0       0]

[0   cons(a) sin(a)   0]

[0   -sin(a)  cos(a)   0]

[0    0        0       1]

a为旋转角度



下面的变化沿y-轴进行旋转:

pic22.gif (1964 bytes)



[x' y' z' 1]=[x y z 1]A  (矩阵相乘)

矩阵A=

[cos(a)  0    -sin(a)  0]

[0       1    0       0]

[sin(a)  0    cos(a)   0]

[0       0        0    1]


下面的变换沿z-轴进行旋转:

pic23.gif (2042 bytes)

[x' y' z' 1]=[x y z 1]A  (矩阵相乘)

矩阵A=

[cos(a)  sin(a)   0    0]

[-sin(a) cos(a)   0   0]

[0       0        1    0]

[0       0        0    1]


关于代码,在Rotate函数中,涉及到的,三维坐标公式变换,我们仅推导其中一种,其他的变换均类似于此推导。

比如 6个面朝向自己中的(z,y,2-x)变换。

假设初始状态是:




由左图变为右图的变换, 假设原点坐标为(x,y,z),首先是立体绕y轴(y轴指向纸面内) 旋转90度,

带入绕y轴的旋转矩阵公式得新坐标(x',y‘,z’)

x'=x*cos(a)+z*sin(a)  y'=y  z'= -sin(a)*x+cos(a)*z  a=90度  带入得 x'=z y'=y z'=-x

又由于要将立方体移入第一象限(只能在3*3*3的方块内,不能越界) 所以向z轴正方向平移两个单位(坐标起始点是0 1 2 ,这里将块转换成点,所以在点坐标上是+2 而不是+3,在块的角度上是+3 在坐标的角度上;在坐标的角度上,上图的边长变成了3个坐标点 2的长度)。这样就是x'=z  y'=y  z'=2-x  这样就得出这个变换的坐标公式为

(z,y,2-x) 同理可得其他5个面朝向自己的坐标变换公式,绕z轴转动的坐标变换公式,也都可依此推导出来。


另外要特别注意在坐标变换中涉及到的编码知识,要明确初始点的坐标和目标点的坐标(初始点经过怎样的变换到达目标点),在绕z轴转动的代码中switch(za),其起始点已经不是 6个面朝向自己中的初始点(i,j,k) 而是6个面朝向变换完成后的目标点(u,v,w) 依此作为绕z轴变换的初始点 ,即在朝向变换完成后的基础上 再进行绕z轴变换 而不是朝向变换之前的点作为z轴变换的初始。


代码中的Conf[7][27][24]就是一个代码库,可以把他看成一个树形库:1、 首选第一层 有7个分支 代表碎片的类型。2、每个类型分支下游27种平移方式。3、每个类型分支下的每个平移方式又有24种旋转方式。

这里变换的编码要转换好,不然很容易错。例如,6个面朝向变换的switch(pla) 中 下边转码是 [0+pla*4] <= [0]  即表示旋转初始态[0] 经过一个6面变换,赋值给一个6面变换完成的位置。而剩下的[pla*4+0]  [pla*4+1]  [pla*4+2] [pla*4+3]的位置 则是在相应的6面变换基础下(6面变换基础:pla*4)的绕z轴变换。诸如此类的编码变换要特别注意不能出错,一错,就整个都错了。  

说明二、

初始化与打表:这里我的初始化是 对a的旋转相应打表而形成的,即我已经有所有a旋转的构形,然后我将a的平移配合a的旋转,对所有a进行DFS,即将这个题目所需要的所有答案都生成出来,存在一个答案库中 vector<string> Ans。然后才开始录入题目输入,然后直接在答案库中作相应匹配,找出相应输出即可。这相当于是一个基于打表的程序,即把所有答案都打表到内存中,然后获取输入匹配输出。

说明三、

关于dfs碎片种类的顺序:刚开始,我是按b~g自然排序初始化的数组,Conf的即 Conf[1]=>b  Conf[2]=>c Conf[3]=>d ...Conf[6]=>g, 结果DFS下来,我的机器环境下,生成出所有的答案竟然要5min之久。但后来发现只要把数量大的碎片排到前面 ,数量小的排到后面,即Conf[6]=>b   把b碎片放到最后(这里有点贪心的味道,先DFS数量大的碎片,再DFS数量小的碎片)。本地环境下,其DFS出所有答案瞬间降低到1min左右(uva 服务器仅12s就算出来了,服务器太强 orz)。其实也容易推论出来,先放大的,对于方块的放置碎片确定性会增大,无论是确定能继续能放还是不能继续放。这样能够 尽早从不能继续放置的情况中回溯回来;而先放小的,总是要DFS比较深的深度上才能意识到结果的不可能性,然后才一步一步回溯过来。

代码1:(更具体的说明就看代码注释吧)

/*
my machine run this program , input the first sample input  need 1 min to finish
but the uva  only need 12s input the all input data to finish , the uva's machine is super orz
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

//it is likely Conf[7][27][24]
string Conf[7][27][24];//begin with 0,and we can use unique  find  for_each function etc.
string hConf[7][27*24];//use for the sort and unique
int RoLen[7][27]={0};//the pieces 's count of the rotate kinds, the RoLen[i][j]<=24  the RoLen[i]-1 is the conf[i] the last rotate kind,RoLen[0]=1,a can not be rotated
int TranLen[7]={0};//the pieces kind and rotate  kind is  the count of the trans kind TranLen[i]<=27
int Len[7]={0};//len[i] is the i's pieces's  all rotate and trans number of kinds that is no repeat
char InA[27+5]={'\0'};
char Pieces[7]={'a','b','c','d','e','f','g'};//begin with 0
int PieNum[7]={4,3,4,4,4,4,4};
string Aform[24]=//all the possible rotated a piece,not include the translate
{
  "a..a..aa...................",
  "..a..a.aa..................",
  "aa.a..a....................",
  "aa..a..a...................",
  "a..aaa.....................",
  "..aaaa.....................",
  "aaa..a.....................",
  "aaaa.......................",
  "..a..a..a........a.........",
  ".................a..a..a..a",
  "..a..a..a..a...............",
  "...........a........a..a..a",
  "..a..a........a........a...",
  ".....a........a.....a..a...",
  "..a........a........a..a...",
  "..a..a.....a........a......",
  "aa.......a........a........",
  "aa........a........a.......",
  "a........a........aa.......",
  ".a........a.......aa.......",
  "aaa......a.................",
  "aaa........a...............",
  "..a......aaa...............",
  "a........aaa..............."
};

std::vector<string> Ans[24];
int AnsCnt=0;
int AnsIndex=0;
string Solid;//now the solid 's  state

//translate the  conf[i][j] the initialization is conf[i][j][0] update to the conf[i][j][k]
int Translate(int cur)
{
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int x=0,y=0,z=0;//the translation's increase
	//enume the  translate increase, x,y,z begin from 0 that is + direction becase the init Conf[cur][0][0] has placed on the (0,0,0),so it can not be translate in - direction,we do not be allowed the negative coordinate values 
	for(x=0;x<=2;x++)
	{
		for(y=0;y<=2;y++)
		{
			for(z=0;z<=2;z++)
			{
				//get the souce point
				int t=0;//souce one dimension value
				int r=0;//destination one dimenesion value
				for(t=0;t<=27-1;t++)
				{
					if(Conf[cur][0][0][t]!='.')//the source point
					{
						i=t%3;
						j=t%9/3;
						k=t/9;
						//according to the i,j,k  and x,y,z  get the u,v,w
						u=i+x;
						v=j+y;
						w=k+z;
						//transform the three dimension to the one dimension
						r=u+v*3+w*9;
						if(u>=3||v>=3||w>=3)
						{
							//this translate is invalid so we should invalid all the string Conf[cur][r][0]
							Conf[cur][0+x+y*3+z*9][0]="xxxxxxxxxxxxxxxxxxxxxxxxxxx";//this is invalid string , in the back of code ,we would sort it to exclude them.
							break;
						}
						//the  trans[0]  translate to the trans[r] the this index<=27-1 , left var???????????
						//note the pkind tkind and rkind 's increase the left second index should not be r,r is one dimension coordinate is not the tkind 
						Conf[cur][0+x+y*3+z*9][0][r]=Conf[cur][0][0][t];//the rotate has not begin so the rotate has only one kind so it is Conf[cur][r][0] (third index),and the translation kind generate has begin so it is Conf[cur][r][0](second index)
					}
				}
			}
		}
	}
	return(0);
}

/*rotate the conf[i]  initialization is conf[i][0][0]  update to the conf[i][j][0]*/
int Rotate(int pkind,int tkind)//peice kind number  and the trans kind number
{
	//x stirng  ,we prune it
	if(Conf[pkind][tkind][0][0]=='x')
	{
		return(0);
	}
	//let the solid 's 6 plane direct to us, our direct to the solid on top view,so we can get the all 6 plane of the solid
	//(x,y,z),(z,y,2-x),(2-x,y,2-z),(2-z,y,x),(x,z,2-y),(x,2-z,y) use the matrix transformation and translation to get these formula
	int pla=0,za=0;//6 planes  and z-axis 4 times
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int oldpla=-1;//flag the pla change
	for(pla=0;pla<=6-1;pla++)
	{
		//get the souce point
		int t=0;//souce one dimension value
		int r=0;//destination one dimenesion value
		int rindex=0;//rkind 's  index in the Conf array
		//pretendend that it must have 24 kinds rotate, how to control the RoLen' change????????
		for(t=0;t<=27-1;t++)
		{
			if(Conf[pkind][tkind][0][t]!='.')
			{
				//get the souce point i,j,k
				i=t%3;
				j=t%9/3;
				k=t/9;
				//use formula (x,y,z),(z,y,2-x),(2-x,y,2-z),(2-z,y,x),(x,z,2-y),(x,2-z,y)
				switch(pla)
				{
					case 0://no rotate this is itself
					u=i;
					v=j;
					w=k;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate  
					break;
					case 1://rotate , the direct is front->top->behind->bottom->front
					//get the destination point u,v,w according to the rotate formula
					u=k;
					v=j;
					w=2-i;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate  
					break;
					case 2://rotate , the direct is front->top->behind->bottom->front
					//get the destination point u,v,w according to the rotate formula
					u=2-i;
					v=j;
					w=2-k;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
					break;
					case 3://rotate , the direct is front->top->behind->bottom->front
					//get the destination point u,v,w according to the rotate formula
					u=2-k;
					v=j;
					w=i;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
					break;
					case 4://rotate , the direct is left -> top -> right -> bottom -> left
					//get the destination point u,v,w according to the rotate formula
					u=i;
					v=k;
					w=2-j;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate					
					break;
					case 5://rotate , the direct is right -> top -> left -> bottom -> right
					//get the destination point u,v,w according to the rotate formula
					u=i;
					v=2-k;
					w=j;
					//Transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
					break;
				}
				//after get the one plane  we can let it around the z-axis rotate 4 times,so we can get the at most all rotate kind = 6*4=24
				for(za=0;za<=4-1;za++)
				{
					//on the 6 plas's fundation  we around z-axis  rotate
					int u1=0,v1=0,w1=0;//z-axis rotate destination coordinate
					int r1=0;//z-axis rotate destination one dimension
					//t loop infulence the RoLen
					//(x,y),(2-y,x),(2-x,2-y),(y,2-x),use the matrix transformation and translation to get these formula
					switch(za)
					{
						case 0://rotate 0 degree aroud the z-axis,so it is it self,although the case 0 is it self but this is the 'zuhe' problem ,so such as pla=0 za=1,so in the za,we must use u,v,w,if pla=0 there is no u,v,w so it will be tend to wrong 
						u1=u;
						v1=v;//note that the source and the destination , our int the before's wrong is u=i v=j w=k, it's very wrong answer. in the correct the source is pla's destination  the destination is the new point
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
						break;
						case 1://rotate,the direct is right -> front -> left -> behind -> right
						//get the destination point u,v,w according to the rotate formula
						u1=2-v;
						v1=u;
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate							
						break;
						case 2://rotate, the direct is right -> front -> left -> behind -> right
						//get the destination point u,v,w according to the rotate formula
						u1=2-u;
						v1=2-v;
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
						break;
						case 3://rotate, the direct is right -> front -> left -> behind -> right
						//get the destination point u,v,w according to the rotate formula
						u1=v;
						v1=2-u;
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
						break;
					}

				}
			}
		}
	}
	return(0);
}
/*trans the Conf to the hConf*/
int Transf()
{
	int i=0,j=0,k=0;
	for(i=0;i<=7-1;i++)
	{
		for(j=0;j<=27-1;j++)
		{
			for(k=0;k<=24-1;k++)
			{
				hConf[i][j*24+k]=Conf[i][j][k];//is j*24+k not j*k
			}
		}
	}
	return(0);
}
/*initialize on the while outside*/
int Init()
{
	//init b~g 's  rotate and trans
	int i=0,j=0,k=0;
	for(i=0;i<=7-1;i++)
	{
		for(j=0;j<=27-1;j++)
		{
			for(k=0;k<=24-1;k++)
			{
				hConf[i][j*k].resize(27,'.');
				Conf[i][j][k].resize(27,'.');
			}
		}
	}
	//the sort influence the running time very important,if the b piece at the index 1 ,the time will increase by 5~6 times
	Conf[6][0][0]="bb.b.......................";//b
	Conf[1][0][0]="ccc.c......................";//c
	Conf[2][0][0]="dd..dd.....................";//d
	Conf[4][0][0]="ee..e.....e................";//e
	Conf[5][0][0]="ff.f........f..............";//f
	Conf[3][0][0]="gg.g......g................";//g

	//rotate b~g
	for(i=1;i<=6;i++)
	{
		//translate the  conf[i][j] the initialization is conf[i][j][0] update to the conf[i][j][k]
		Translate(i);
		for(j=0;j<=27-1;j++)//27 kinds of the translate
		{
			//rotate
			Rotate(i,j);//rotate the conf[i]  initialization is conf[i][0][0]  update to the conf[i][j][0+increase]
		}
	}
	Transf();
	for(i=1;i<=6;i++)
	{
		sort(hConf[i],hConf[i]+27*24);
		Len[i]=unique(hConf[i],hConf[i]+27*24)-hConf[i];//hConf[i][0] is "...................", hConf[i][Len[i]-1] is "xxxxxxxxxxxxxxxx" and others is valid string, we would exclude the hConf[i][0] and the hConf[i][Len[i]-1] in the DFS
	}
	return(0);
}

/*judge the fitable .&letter  and .&. and letter&. is true, letter&letter is false*/
bool Isfit(string &a,string &b)
{
	int i=0;
	//if(strcpy(b,"...........................")==0) return(false);
	for(i=0;i<=27-1;i++)
	{
		if(a[i]!='.'&&b[i]!='.')
		{
			return(false);
		}
	}
	return(true);
}

/*fill the piece into the solid*/
int FillPie(string &a,string &b)
{
	int i=0;
	for(i=0;i<=27-1;i++)
	{
		if(b[i]!='.')
		{
			a[i]=b[i];
		}
	}
	return(0);
}

/*unfill the piece in the solid*/
int unFillPie(string &a,string &b)
{
	int i=0;
	for(i=0;i<=27-1;i++)
	{
		if(b[i]!='.')
		{
			a[i]='.';
		}
	}
	return(0);
}

/*dfs the piece kind that is to say the DFS's depth is piece*/
int DFS(int cur)//the cur is the peice kind index,   0<= cur <=6
{
	if(cur>=7)//the solid has filled up with the letter
	{
		Ans[AnsIndex].push_back(Solid);
	}
	else
	{
		int i=0;
		for(i=1;i<=Len[cur]-1-1;i++)//  the index i=0 Conf is ".........................." and index  Len[i]-1 Conf is "xxxxxxxxxxxxxxxx"
		{
			if(Isfit(Solid,hConf[cur][i]))
			{
				FillPie(Solid,hConf[cur][i]);
				DFS(cur+1);
				unFillPie(Solid,hConf[cur][i]);
			}
		}
	}
	return(0);
}
/*Init all the answer that we need in this problem*/
int InitAns()
{
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int x=0,y=0,z=0;//the translation's increase
	int t=0;//souce one dimension value
	int r=0;//destination one dimenesion value
	int aindex=0;//the index of the Aform
	//translate the all a pieces(it can not be rotated) in the Aform, x,y,z begin with -2 because in the Aform,the a piece is not placed on the (0,0,0) so it can be + direct or -direct to translate 
	for(aindex=0;aindex<=24-1;aindex++)
	{
		for(x=-2;x<=2;x++)
		{
			for(y=-2;y<=2;y++)
			{
				for(z=-2;z<=2;z++)//when x=y=z=0 the translate it is it self
				{
					Solid="...........................";
					for(t=0;t<=27-1;t++)
					{
						if(Aform[aindex][t]!='.')
						{
							i=t%3;
							j=t%9/3;
							k=t/9;
							//according to the i,j,k  and x,y,z  get the u,v,w
							u=i+x;
							v=j+y;
							w=k+z;
							//transform the three dimension to the one dimension
							r=u+v*3+w*9;
							if(u>=3||v>=3||w>=3||u<0||v<0||w<0)
							{
								break;
							}
							Solid[r]=Aform[aindex][t];
						}
					}
					if(t==27)//the a translate is success
					{
						AnsIndex=aindex;
						DFS(1);
					}
				}
			}
		}
	}
	return(0);
}

/*print the answer*/
void printAns(string &a)
{
	printf("%s\n",a.c_str() );//cout is wasting time
}

/*find the answer from the Ans lib*/
int FindAns()
{
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int x=0,y=0,z=0;//the translation's increase
	int t=0;//souce one dimension value
	int r=0;//destination one dimenesion value
	//translate the InA
	for(x=-2;x<=2;x++)//the InA is not placed on the (0,0,0) so it can be translate in - direct
	{
		for(y=-2;y<=2;y++)
		{
			for(z=-2;z<=2;z++)
			{
				Solid="...........................";//why????????
				for(t=0;t<=27-1;t++)
				{
					if(InA[t]!='.')
					{
						i=t%3;
						j=t%9/3;
						k=t/9;
						//according to the i,j,k  and x,y,z  get the u,v,w
						u=i+x;
						v=j+y;
						w=k+z;
						//transform the three dimension to the one dimension
						r=u+v*3+w*9;
						if(u>=3||v>=3||w>=3||u<0||v<0||w<0)
						{
							break;
						}
						Solid[r]=InA[t];
					}
				}
				if(t==27)//the translate is success
				{
					int findindex=find(Aform,Aform+24,Solid)-Aform;//find the one match the Aform, and the Aform is only rotate not the translate kind,so find the one rotate is ok,the other translate kind Ans is all in this rotate Ans(please look at the InitAns()) so we can take a break
					if(findindex!=24)//find the answer
					{
						for_each(Ans[findindex].begin(),Ans[findindex].end(),printAns);
						printf("\n");
						break;
					}
				}
			}
		}
	}
	return(0);
}

/*for test*/
int test()
{
	Init();
	return(0);
}

/*main process*/
int MainProc()
{
	//init
	Init();
	//Init all the answer that we need in this problem
	InitAns();
	while(scanf("%s",InA)!=EOF)
	{
		//find the ans in the all answer lib
		FindAns();
	}
	return(0);
}

int main(int argc, char const *argv[])
{
	/* code */
	//test();
	MainProc();
	return 0;
}


代码2说明:

关于剪枝的实现:这里如何找到,不可填的空缺块呢,对于不可填,我们可以这样理解,一个空块在三维空间的 前 后 左 右 上 下相邻的 都是非空块 或者是 越界不可填的块,那么无论它在后面的DFS怎么填 都不可能填到这个空块,因为最小的碎片b都是连通的3个方块,其余碎片更是4个方块,我们据此定义成为封闭空块,其大小为一个小方块。进一步想如果在DFS一个当前状态的立体中,存在一个最小的封闭空块,其小方块数量<=2(意味着最小的b碎片都填不进去),那么这个封闭空块是不可填的(其实这个还要看DFS的碎片种类的顺序,只是最小的b碎片放到了最后才DFS到,所以可以这样直接写)。这样我们在大DFS中 再加入一个小dfs 用于寻找当前立体中的封闭连通空块(dfs连通算法),然后判断其最小封闭连通空块的大小,从而尽早剪枝掉不可能填满立体的情况的深搜路径。


代码2:(详细细节见代码注释)

/*
my machine run this program , input the first sample input  need 1 min to finish
but the uva  only need 12s input the all input data to finish , the uva's machine is super orz
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

//it is likely Conf[7][27][24]
string Conf[7][27][24];//begin with 0,and we can use unique  find  for_each function etc.
string hConf[7][27*24];//use for the sort and unique
int RoLen[7][27]={0};//the pieces 's count of the rotate kinds, the RoLen[i][j]<=24  the RoLen[i]-1 is the conf[i] the last rotate kind,RoLen[0]=1,a can not be rotated
int TranLen[7]={0};//the pieces kind and rotate  kind is  the count of the trans kind TranLen[i]<=27
int Len[7]={0};//len[i] is the i's pieces's  all rotate and trans number of kinds that is no repeat
char InA[27+5]={'\0'};
char Pieces[7]={'a','b','c','d','e','f','g'};//begin with 0
int PieNum[7]={4,3,4,4,4,4,4};
string Aform[24]=//all the possible rotated a piece,not include the translate
{
  "a..a..aa...................",
  "..a..a.aa..................",
  "aa.a..a....................",
  "aa..a..a...................",
  "a..aaa.....................",
  "..aaaa.....................",
  "aaa..a.....................",
  "aaaa.......................",
  "..a..a..a........a.........",
  ".................a..a..a..a",
  "..a..a..a..a...............",
  "...........a........a..a..a",
  "..a..a........a........a...",
  ".....a........a.....a..a...",
  "..a........a........a..a...",
  "..a..a.....a........a......",
  "aa.......a........a........",
  "aa........a........a.......",
  "a........a........aa.......",
  ".a........a.......aa.......",
  "aaa......a.................",
  "aaa........a...............",
  "..a......aaa...............",
  "a........aaa..............."
};

std::vector<string> Ans[24];
int AnsCnt=0;
int AnsIndex=0;
string Solid;//now the solid 's  state

//translate the  conf[i][j] the initialization is conf[i][j][0] update to the conf[i][j][k]
int Translate(int cur)
{
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int x=0,y=0,z=0;//the translation's increase
	//enume the  translate increase, x,y,z begin from 0 that is + direction becase the init Conf[cur][0][0] has placed on the (0,0,0),so it can not be translate in - direction,we do not be allowed the negative coordinate values 
	for(x=0;x<=2;x++)
	{
		for(y=0;y<=2;y++)
		{
			for(z=0;z<=2;z++)
			{
				//get the souce point
				int t=0;//souce one dimension value
				int r=0;//destination one dimenesion value
				for(t=0;t<=27-1;t++)
				{
					if(Conf[cur][0][0][t]!='.')//the source point
					{
						i=t%3;
						j=t%9/3;
						k=t/9;
						//according to the i,j,k  and x,y,z  get the u,v,w
						u=i+x;
						v=j+y;
						w=k+z;
						//transform the three dimension to the one dimension
						r=u+v*3+w*9;
						if(u>=3||v>=3||w>=3)
						{
							//this translate is invalid so we should invalid all the string Conf[cur][r][0]
							Conf[cur][0+x+y*3+z*9][0]="xxxxxxxxxxxxxxxxxxxxxxxxxxx";//this is invalid string , in the back of code ,we would sort it to exclude them.
							break;
						}
						//the  trans[0]  translate to the trans[r] the this index<=27-1 , left var???????????
						//note the pkind tkind and rkind 's increase the left second index should not be r,r is one dimension coordinate is not the tkind 
						Conf[cur][0+x+y*3+z*9][0][r]=Conf[cur][0][0][t];//the rotate has not begin so the rotate has only one kind so it is Conf[cur][r][0] (third index),and the translation kind generate has begin so it is Conf[cur][r][0](second index)
					}
				}
			}
		}
	}
	return(0);
}

/*rotate the conf[i]  initialization is conf[i][0][0]  update to the conf[i][j][0]*/
int Rotate(int pkind,int tkind)//peice kind number  and the trans kind number
{
	//x stirng  ,we prune it
	if(Conf[pkind][tkind][0][0]=='x')
	{
		return(0);
	}
	//let the solid 's 6 plane direct to us, our direct to the solid on top view,so we can get the all 6 plane of the solid
	//(x,y,z),(z,y,2-x),(2-x,y,2-z),(2-z,y,x),(x,z,2-y),(x,2-z,y) use the matrix transformation and translation to get these formula
	int pla=0,za=0;//6 planes  and z-axis 4 times
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int oldpla=-1;//flag the pla change
	for(pla=0;pla<=6-1;pla++)
	{
		//get the souce point
		int t=0;//souce one dimension value
		int r=0;//destination one dimenesion value
		int rindex=0;//rkind 's  index in the Conf array
		//pretendend that it must have 24 kinds rotate, how to control the RoLen' change????????
		for(t=0;t<=27-1;t++)
		{
			if(Conf[pkind][tkind][0][t]!='.')
			{
				//get the souce point i,j,k
				i=t%3;
				j=t%9/3;
				k=t/9;
				//use formula (x,y,z),(z,y,2-x),(2-x,y,2-z),(2-z,y,x),(x,z,2-y),(x,2-z,y)
				switch(pla)
				{
					case 0://no rotate this is itself
					u=i;
					v=j;
					w=k;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate  
					break;
					case 1://rotate , the direct is front->top->behind->bottom->front
					//get the destination point u,v,w according to the rotate formula
					u=k;
					v=j;
					w=2-i;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate  
					break;
					case 2://rotate , the direct is front->top->behind->bottom->front
					//get the destination point u,v,w according to the rotate formula
					u=2-i;
					v=j;
					w=2-k;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
					break;
					case 3://rotate , the direct is front->top->behind->bottom->front
					//get the destination point u,v,w according to the rotate formula
					u=2-k;
					v=j;
					w=i;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
					break;
					case 4://rotate , the direct is left -> top -> right -> bottom -> left
					//get the destination point u,v,w according to the rotate formula
					u=i;
					v=k;
					w=2-j;
					//transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate					
					break;
					case 5://rotate , the direct is right -> top -> left -> bottom -> right
					//get the destination point u,v,w according to the rotate formula
					u=i;
					v=2-k;
					w=j;
					//Transform the three dimension to the one dimension
					r=u+v*3+w*9;
					//source -> destination ,so so pieces has rotated
					Conf[pkind][tkind][0+pla*4][r]=Conf[pkind][tkind][0][t];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
					break;
				}
				//after get the one plane  we can let it around the z-axis rotate 4 times,so we can get the at most all rotate kind = 6*4=24
				for(za=0;za<=4-1;za++)
				{
					//on the 6 plas's fundation  we around z-axis  rotate
					int u1=0,v1=0,w1=0;//z-axis rotate destination coordinate
					int r1=0;//z-axis rotate destination one dimension
					//t loop infulence the RoLen
					//(x,y),(2-y,x),(2-x,2-y),(y,2-x),use the matrix transformation and translation to get these formula
					switch(za)
					{
						case 0://rotate 0 degree aroud the z-axis,so it is it self,although the case 0 is it self but this is the 'zuhe' problem ,so such as pla=0 za=1,so in the za,we must use u,v,w,if pla=0 there is no u,v,w so it will be tend to wrong 
						u1=u;
						v1=v;//note that the source and the destination , our int the before's wrong is u=i v=j w=k, it's very wrong answer. in the correct the source is pla's destination  the destination is the new point
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
						break;
						case 1://rotate,the direct is right -> front -> left -> behind -> right
						//get the destination point u,v,w according to the rotate formula
						u1=2-v;
						v1=u;
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate							
						break;
						case 2://rotate, the direct is right -> front -> left -> behind -> right
						//get the destination point u,v,w according to the rotate formula
						u1=2-u;
						v1=2-v;
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
						break;
						case 3://rotate, the direct is right -> front -> left -> behind -> right
						//get the destination point u,v,w according to the rotate formula
						u1=v;
						v1=2-u;
						w1=w;
						//transform the three dimension to the one dimension
						r1=u1+v1*3+w1*9;
						//source -> destination ,so so pieces has rotated
						Conf[pkind][tkind][0+pla*4+za][r1]=Conf[pkind][tkind][pla*4][r];//not the [t] <- [t], because rotate has changed the rotated coordinate,it is different from the translate
						break;
					}

				}
			}
		}
	}
	return(0);
}
/*trans the Conf to the hConf*/
int Transf()
{
	int i=0,j=0,k=0;
	for(i=0;i<=7-1;i++)
	{
		for(j=0;j<=27-1;j++)
		{
			for(k=0;k<=24-1;k++)
			{
				hConf[i][j*24+k]=Conf[i][j][k];//is j*24+k not j*k
			}
		}
	}
	return(0);
}
/*initialize on the while outside*/
int Init()
{
	//init b~g 's  rotate and trans
	int i=0,j=0,k=0;
	for(i=0;i<=7-1;i++)
	{
		for(j=0;j<=27-1;j++)
		{
			for(k=0;k<=24-1;k++)
			{
				hConf[i][j*k].resize(27,'.');
				Conf[i][j][k].resize(27,'.');
			}
		}
	}
	//the sort influence the running time very important,if the b piece at the index 1 ,the time will increase by 5~6 times
	Conf[6][0][0]="bb.b.......................";//b
	Conf[1][0][0]="ccc.c......................";//c
	Conf[2][0][0]="dd..dd.....................";//d
	Conf[4][0][0]="ee..e.....e................";//e
	Conf[5][0][0]="ff.f........f..............";//f
	Conf[3][0][0]="gg.g......g................";//g

	//rotate b~g
	for(i=1;i<=6;i++)
	{
		//translate the  conf[i][j] the initialization is conf[i][j][0] update to the conf[i][j][k]
		Translate(i);
		for(j=0;j<=27-1;j++)//27 kinds of the translate
		{
			//rotate
			Rotate(i,j);//rotate the conf[i]  initialization is conf[i][0][0]  update to the conf[i][j][0+increase]
		}
	}
	Transf();
	for(i=1;i<=6;i++)
	{
		sort(hConf[i],hConf[i]+27*24);
		Len[i]=unique(hConf[i],hConf[i]+27*24)-hConf[i];//hConf[i][0] is "...................", hConf[i][Len[i]-1] is "xxxxxxxxxxxxxxxx" and others is valid string, we would exclude the hConf[i][0] and the hConf[i][Len[i]-1] in the DFS
	}
	return(0);
}

/*judge the fitable .&letter  and .&. and letter&. is true, letter&letter is false*/
bool Isfit(string &a,string &b)
{
	int i=0;
	//if(strcpy(b,"...........................")==0) return(false);
	for(i=0;i<=27-1;i++)
	{
		if(a[i]!='.'&&b[i]!='.')
		{
			return(false);
		}
	}
	return(true);
}

/*fill the piece into the solid*/
int FillPie(string &a,string &b)
{
	int i=0;
	for(i=0;i<=27-1;i++)
	{
		if(b[i]!='.')
		{
			a[i]=b[i];
		}
	}
	return(0);
}

/*unfill the piece in the solid*/
int unFillPie(string &a,string &b)
{
	int i=0;
	for(i=0;i<=27-1;i++)
	{
		if(b[i]!='.')
		{
			a[i]='.';
		}
	}
	return(0);
}
int MinEm=0;//min empty connencted block numebr
int EmNum=0;//the num of the empty connencted block numebr
string vis;//the Solid's copy
/*dfs the Solid,find the min empty connected block number*/
int dfsfind(int cur)
{
	//transform to the three dimension coordinate
	int i=cur%3;//source point
	int j=cur%9/3;
	int k=cur/9;
	int u=0,v=0,w=0;//destination point
	int cur1=0;//the destination one dimension
	//overflow the boundary
	if(i>=3||j>=3||k>=3||i<0||j<0||k<0)
	{
		return(0);
	}
	//not the empty block
	if(vis[cur]!='.')
	{
		return(0);
	}
	vis[cur]='x';//has visited
	EmNum++;//this is a empty block
	//x-axis +1 direction
	u=i+1;
	v=j;
	w=k;
	cur1=u+v*3+w*9;
	dfsfind(cur1);
	//x-1
	u=i-1;
	v=j;
	w=k;
	cur1=u+v*3+w*9;
	dfsfind(cur1);
	//y+1
	u=i;
	v=j+1;
	w=k;
	cur1=u+v*3+w*9;
	dfsfind(cur1);
	//y-1
	u=i;
	v=j-1;
	w=k;
	cur1=u+v*3+w*9;
	dfsfind(cur1);
	//z+1
	u=i;
	v=j;
	w=k+1;
	cur1=u+v*3+w*9;
	dfsfind(cur1);
	//z-1
	u=i;
	v=j;
	w=k-1;
	cur1=u+v*3+w*9;
	dfsfind(cur1);
	return(0);
}
/*check the the Solid,the empty block is not filled forever . that is to say ,it is surrounded by no empty block*/
bool CheckEm()
{
	//dfs find the min empty connected block number
	int t=0;
	vis=Solid;
	MinEm=100;
	EmNum=0;
	for(t=0;t<=27-1;t++)
	{
		if(vis[t]=='.')//empty block
		{
			EmNum=0;
			dfsfind(t);//t is the index of the Solid or one dimension coordinate
			if(EmNum<MinEm)
			{
				MinEm=EmNum;
			}
		}
	}
	//test
	//printf("%d\n",MinEm);
	if(MinEm<=2)//the if the 2-lenth empty connected ,it would be not filled forever,because the b piece at the last place
	{
		return(false);
	}
	else
	{
		return(true);
	}
}
/*dfs the piece kind that is to say the DFS's depth is piece*/
int DFS(int cur)//the cur is the peice kind index,   0<= cur <=6
{
    //check the the Solid,the empty block is not filled forever . that is to say ,it is surrounded by no empty block
	if(!CheckEm())
	{
		return(0);
	}
	if(cur>=7)//the solid has filled up with the letter
	{
		Ans[AnsIndex].push_back(Solid);
	}
	else
	{
		int i=0;
		for(i=1;i<=Len[cur]-1-1;i++)//  the index i=0 Conf is ".........................." and index  Len[i]-1 Conf is "xxxxxxxxxxxxxxxx"
		{
			if(Isfit(Solid,hConf[cur][i]))
			{
				FillPie(Solid,hConf[cur][i]);
				DFS(cur+1);
				unFillPie(Solid,hConf[cur][i]);
			}
		}
	}
	return(0);
}
/*Init all the answer that we need in this problem*/
int InitAns()
{
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int x=0,y=0,z=0;//the translation's increase
	int t=0;//souce one dimension value
	int r=0;//destination one dimenesion value
	int aindex=0;//the index of the Aform
	//translate the all a pieces(it can not be rotated) in the Aform, x,y,z begin with -2 because in the Aform,the a piece is not placed on the (0,0,0) so it can be + direct or -direct to translate 
	for(aindex=0;aindex<=24-1;aindex++)
	{
		for(x=-2;x<=2;x++)
		{
			for(y=-2;y<=2;y++)
			{
				for(z=-2;z<=2;z++)//when x=y=z=0 the translate it is it self
				{
					Solid="...........................";
					for(t=0;t<=27-1;t++)
					{
						if(Aform[aindex][t]!='.')
						{
							i=t%3;
							j=t%9/3;
							k=t/9;
							//according to the i,j,k  and x,y,z  get the u,v,w
							u=i+x;
							v=j+y;
							w=k+z;
							//transform the three dimension to the one dimension
							r=u+v*3+w*9;
							if(u>=3||v>=3||w>=3||u<0||v<0||w<0)
							{
								break;
							}
							Solid[r]=Aform[aindex][t];
						}
					}
					if(t==27)//the a translate is success
					{
						AnsIndex=aindex;
						DFS(1);
					}
				}
			}
		}
	}
	return(0);
}

/*print the answer*/
void printAns(string &a)
{
	printf("%s\n",a.c_str() );//cout is wasting time
}

/*find the answer from the Ans lib*/
int FindAns()
{
	int u=0,v=0,w=0;//the destination of the translation point
	int i=0,j=0,k=0;//the souce of the translation point
	int x=0,y=0,z=0;//the translation's increase
	int t=0;//souce one dimension value
	int r=0;//destination one dimenesion value
	//translate the InA
	for(x=-2;x<=2;x++)//the InA is not placed on the (0,0,0) so it can be translate in - direct
	{
		for(y=-2;y<=2;y++)
		{
			for(z=-2;z<=2;z++)
			{
				Solid="...........................";//why????????
				for(t=0;t<=27-1;t++)
				{
					if(InA[t]!='.')
					{
						i=t%3;
						j=t%9/3;
						k=t/9;
						//according to the i,j,k  and x,y,z  get the u,v,w
						u=i+x;
						v=j+y;
						w=k+z;
						//transform the three dimension to the one dimension
						r=u+v*3+w*9;
						if(u>=3||v>=3||w>=3||u<0||v<0||w<0)
						{
							break;
						}
						Solid[r]=InA[t];
					}
				}
				if(t==27)//the translate is success
				{
					int findindex=find(Aform,Aform+24,Solid)-Aform;//find the one match the Aform, and the Aform is only rotate not the translate kind,so find the one rotate is ok,the other translate kind Ans is all in this rotate Ans(please look at the InitAns()) so we can take a break
					if(findindex!=24)//find the answer
					{
						for_each(Ans[findindex].begin(),Ans[findindex].end(),printAns);
						printf("\n");
						break;
					}
				}
			}
		}
	}
	return(0);
}

/*for test*/
int test()
{
	Init();
	return(0);
}

/*main process*/
int MainProc()
{
	//init
	Init();
	//Init all the answer that we need in this problem
	InitAns();
	while(scanf("%s",InA)!=EOF)
	{
		//find the ans in the all answer lib
		FindAns();
	}
	return(0);
}

int main(int argc, char const *argv[])
{
	/* code */
	//test();
	MainProc();
	return 0;
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值