sdutoj 2606 Rubik’s cube

时间:2023-03-09 17:04:28
sdutoj 2606 Rubik’s cube

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2606

Rubik’s cube

Time Limit: 2000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

    Flabby is addicted to Rubik’s cube. He has many kinds of Rubik’s cube and plays them well. He can reorder a Rubik’s cube in a few seconds. He is famous for playing Rubik’s cube and attracts many young girls. One of his friends, Ant, is jealous of him and decides to let him be embarrassed in public. Ant makes a small Rubik’s cube by himself. This small magic is 2*2*2 and is drawn in blue and red, as shown in Figure 1. 
                                                              sdutoj 2606 Rubik’s cube
    This Rubik’s cube can be rotated as well as other kinds of magic cube. Each of six faces can rotated clockwise and counterclockwise. Each 90 degree rotation on any face is counted as one step. The Rubik’s cube which has been reordered has only one color on each face.
     Ant is a clever boy. Sometimes, he can make a Rubik’s cube which can be reordered in a few steps. He can also make a Rubik’s cube which can’t be reordered in any way.
                                                            sdutoj 2606 Rubik’s cube
                                                                 sdutoj 2606 Rubik’s cube
Flabby knows what Ant thinks in his mind. He knows that you are a good programmer and asks you for help. Tell him whether this special Rubik’s cube can be reordered in a few steps.

输入

    In the input file, the first line is an integer T which is the number of test case. For each test case, there is 6 lines of integers describe the Rubik’s cube. For each line, there are four integers. Pij which is shown in Figure 3 corresponds to the jth integer of the ith line. If Pij is 0, it means the corresponding place is red. If Pij is 1, it means the corresponding place is blue. 

输出

    If the magic cube can be reordered in n steps, output the minimum n in a line. If the magic cube cannot be reordered, output “IMPOSSIBLE!” in a line. 

示例输入

3
0 0 0 0
0 1 0 1
1 1 0 0
1 0 1 0
1 1 0 0
1 1 1 1
0 0 1 1
1 1 0 0
0 1 0 1
1 0 0 0
1 1 1 1
0 1 0 0
1 0 1 1
0 1 0 0
0 0 0 0
1 1 1 1
1 0 0 0
0 1 1 1

示例输出

1
IMPOSSIBLE!
8

提示

来源

 2013年山东省第四届ACM大学生程序设计竞赛

示例程序

官方代码:

 /**
23
10
23
10
010101
323232
01
32
6
3
412
5
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
char md[][];
#define MAX 20000000
bool flag[][][][][][];
struct M
{
short int num[];
short int lv;
void print()
{
int i;
for(i = ; i < ; i++)
printf("%d ",num[i]);
printf("\n");
return;
}
}queue[MAX];
M (*funp[])(M m);
bool check(M m)
{
int i;
for(i = ; i < ; i++)
{
if(m.num[i] != && m.num[i] != )
return false;
}
return true;
}
M turnY_L(M tm)
{
M m;
m.num[] = (tm.num[] >> ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) << ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) << ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) << ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) >> ) | ((tm.num[] & ) >> );
m.num[] = tm.num[];
return m;
}
M turnY_R(M tm)
{
M m;
m.num[] = ((tm.num[] & ) << ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) >> ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) >> ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) << ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) << ) | ((tm.num[] & ) >> );
m.num[] = tm.num[];
return m;
} /*------------------------------------------------------------------------------------*/ M turnX_L(M tm)
{
M m;
m.num[] = (tm.num[] >> ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | ((tm.num[] & ) << ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | ((tm.num[] & ));
m.num[] = tm.num[];
m.num[] = (tm.num[] & ) | ((tm.num[] & ));
m.num[] = (tm.num[] & ) | ((tm.num[] & ) >> ) | ((tm.num[] & ) << );
return m;
}
M turnX_R(M tm)
{
M m;
m.num[] = ((tm.num[] & ) << ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | ((tm.num[] & ));
m.num[] = (tm.num[] & ) | ((tm.num[] & ) >> ) | ((tm.num[] & ) << );
m.num[] = tm.num[];
m.num[] = (tm.num[] & ) | ((tm.num[] & ) >> ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | ((tm.num[] & ));
return m;
} M turnZ_L(M tm)
{
M m;
m.num[] = (tm.num[] >> ) | ((tm.num[] & ) << );
m.num[] = (tm.num[] & ) | (tm.num[] & );
m.num[] = (tm.num[] & ) | (tm.num[] & );
m.num[] = tm.num[];
m.num[] = (tm.num[] & ) | (tm.num[] & );
m.num[] = (tm.num[] & ) | (tm.num[] & );
return m;
}
M turnZ_R(M tm)
{
M m;
m.num[] = ((tm.num[] & )<< ) | ((tm.num[] & ) >> );
m.num[] = (tm.num[] & ) | (tm.num[] & );
m.num[] = (tm.num[] & ) | (tm.num[] & );
m.num[] = tm.num[];
m.num[] = (tm.num[] & ) | (tm.num[] & );
m.num[] = (tm.num[] & ) | (tm.num[] & );
return m;
} void record_flag(int num1,int num2,int num3,int num4,int num5,int num6)
{
//printf("%d %d %d %d %d %d\n",num1,num2,num3,num4,num5,num6);
flag[num1][num2][num3][num4][num5][num6] = ;
flag[num4][num1][(num3>>)|((num3&)<<)][num6][((num5&)<<)+((num3&)>>)][num2] = ;
flag[num6][num4][((num3&)<<)+((num3&)>>)][num2][((num5&)<<)+((num5&)>>)][num1] = ;
flag[num2][num6][((num3&)<<)|((num3&)>>)][num1][(num5>>)+((num5&)<<)][num4] = ;
return;
}
int Search(M m)
{
funp[] = turnX_L;
funp[] = turnX_R;
funp[] = turnY_L;
funp[] = turnY_R;
funp[] = turnZ_L;
funp[] = turnZ_R;
M tmp,tm;
int front,rear,i;
front = rear = ;
memset(flag,,sizeof(flag));
m.lv = ;
queue[rear++] = m;
record_flag(m.num[],m.num[],m.num[],m.num[],m.num[],m.num[]);
while(front < rear)
{
tmp = queue[front++];
if(check(tmp))
return tmp.lv;
for(i = ; i < ; i++)
{
tm = funp[i](tmp);
tm.lv = tmp.lv + ;
if(flag[tm.num[]][tm.num[]][tm.num[]][tm.num[]][tm.num[]][tm.num[]] == )
{
queue[rear++] = tm;
record_flag(tm.num[],tm.num[],tm.num[],tm.num[],tm.num[],tm.num[]);
}
}
}
return -;
} int main()
{
int T,i,n1,n2,n3,n4,ans;
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
scanf("%d",&T);
M m;
while(T--)
{
for(i = ; i < ; i++)
{
scanf("%d%d%d%d",&n1,&n2,&n3,&n4);
m.num[i] = n1 + (n2 << ) + (n3 << ) + (n4 << );
// printf("%d\n",m.num[i]);
}
ans = Search(m);
if(ans != -)
printf("%d\n", ans);
else
printf("IMPOSSIBLE!\n");
}
return ;
}

官方数据生成代码:

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <ctime>
#include <cstdlib>
using namespace std;
int a[] = {,,,,,,,,,,,,,,,,,,,,,,,};
int main()
{
int t1,t2,i;
freopen("data.in","w",stdout);
int T = ;
printf("%d\n",T);
srand();
while(T--)
{ t1 = rand()%;
do
{
t2 = ((rand()%)*(rand()%))%;
}while(a[t1]==a[t2]);
a[t1] = a[t1] ^ a[t2];
a[t2] = a[t1] ^ a[t2];
a[t1] = a[t1] ^ a[t2];
for(i = ; i < ; i++)
{
printf("%d",a[i]);
if(i % == )
printf("\n");
else
printf(" ");
}
printf("\n");
}
return ;
}