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. 
                                                              
    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.
                                                            
                                                                 
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 ;
}

最新文章

  1. 深刻理解:C#中的委托、事件
  2. Android-webview和js互相调用
  3. eclipse Swt编程—窗口小部件widget
  4. 将asp.net webapi的运行时版本由4.0升级到4.5.1时遇到的问题及解决
  5. 《HelloGitHub月刊》第05期
  6. phpcms模板标签整理
  7. golang make the first character in a string lowercase/uppercase
  8. 在win7下php查询数据库, 连接被重置
  9. linux系统下phpstudy里的mysql使用方法
  10. 浅析CompareAndSet(CAS)
  11. C++常用宏
  12. elbow 求拐点
  13. java I/O系统 LineNumberReader类
  14. c# Findwindow sendMessage
  15. express安装及使用(windows系统)
  16. 对SQL SERVER数据类型理解最好的一篇文章
  17. JS重要的内置对象
  18. laravel bald视图控制流与子视图
  19. Eclipse apk 签名
  20. UVALive - 6837 Kruskal+一点性质(暴力枚举)

热门文章

  1. Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command &#39;&#39; received错误
  2. GO语言练习:多返回值函数
  3. Node.js前端自动化工具:gulp
  4. 关于IE9中webdiriver使用autoit上传文件报错
  5. hdu 2102 BFS
  6. Linux_MySql安装
  7. VC 解密OUTLOOK pop3保存注册表密码
  8. Codeforces Round #196 (Div. 2)
  9. 常用js或jq效果汇总
  10. gvim配置