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

sdutoj 2606 Rubik’s cube的更多相关文章

  1. POJ 1955 Rubik's Cube

    暴力模拟就好了.... vim写代码真费事,手都写酸了... Rubik's Cube Time Limit: 1000MS   Memory Limit: 30000K Total Submissi ...

  2. The Mathematics of the Rubik’s Cube

    https://web.mit.edu/sp.268/www/rubik.pdf Introduction to Group Theory and Permutation Puzzles March ...

  3. HDU 5836 Rubik's Cube BFS

    Rubik's Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5836 Description As we all know, Zhu is ...

  4. hduoj 3459 Rubik 2×2×2

    http://acm.hdu.edu.cn/showproblem.php?pid=3459 Rubik 2×2×2 Time Limit: 10000/5000 MS (Java/Others)   ...

  5. squee_spoon and his Cube VI(贪心,找不含一组字符串的最大长度+kmp)

    1818: squee_spoon and his Cube VI Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 77  Solved: 22Subm ...

  6. HDU5983Pocket Cube

    Pocket Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  7. Pocket Cube

    Pocket Cube http://acm.hdu.edu.cn/showproblem.php?pid=5983 Time Limit: 2000/1000 MS (Java/Others)    ...

  8. HDU 5292 Pocket Cube 结论题

    Pocket Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5292 Description Pocket Cube is the 2×2× ...

  9. squee_spoon and his Cube VI---郑大校赛(求最长子串)

    市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...

随机推荐

  1. 关于Go,你可能不注意的7件事(转的)

    http://tonybai.com/2015/09/17/7-things-you-may-not-pay-attation-to-in-go/ code https://github.com/bi ...

  2. PHP slim restfull框架nginx 配置

    http://docs.slimframework.com/ 下载地址这个东西很不错,照到官方的例子做 <?php require 'vendor/autoload.php'; $app = n ...

  3. 兼容所有浏览器的设为首页收藏本站js代码

    大家发现传统的收藏本站按钮在360浏览器下面没有效果了,但是360浏览器用户群却非常之大.所以我们在网上找到一个兼容所有浏览器的收藏本站解决方案,具体功能如下: 设为首页 和 收藏本站js代码 兼容I ...

  4. iOS性能优化:Instruments使用实战

    iOS性能优化:Instruments使用实战   最近采用Instruments 来分析整个应用程序的性能.发现很多有意思的点,以及性能优化和一些分析性能消耗的技巧,小结如下. Instrument ...

  5. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  6. 优化MySchool总结习题

    1) 在SQL Server 中,为数据库表建立索引能够( C ). A. 防止非法的删除操作 B. 防止非法的插入操作 C. 提高查询性能 D. 节约数据库的磁盘空间 解析:索引的作用是通过使用索引 ...

  7. C++ 单元测试 Cpputest

    参见:http://cpputest.github.io/ 1. CppUTest是一个C/C++单元测试框架,可以在C/C++项目中使命. 2. 第一个测试 新建一个cpp 文件,包含一个TEST_ ...

  8. 简单的自定义Adapter

    import android.content.Context; import android.view.LayoutInflater; import android.view.View; import ...

  9. HDU2563 递推

    统计问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  10. ZK 代码自动提示

    1.设置xsd 打开eclipse,Window-Preference,进行如下设置: 2.创建zul文件 (1)打开File—New—Other窗口,新建XML File文件: (2)选择新建文件所 ...