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. URAL 1658. Sum of Digits(DP)

    题目链接 隔了一年零三个月,重新刷URAL,这题挺麻烦的输出路径.输出路径挺扯的,乱写了写乱改改就A了...我本来想用很靠谱,记录每一条路径的,然后输出最小的,结果Tle,然后我使劲水水又过了一组,发 ...

  2. 【hdu1394】Minimum Inversion Number

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

  3. 好的 iOS 代码习惯

    一,使用别人的框架时,尽量在退出时移除框架创建的对象 if (_giftToastView) { [_giftToastView removeFromSuperview]; _giftToastVie ...

  4. Spring整合Hibernate之AnnotationSessionFactoryBean与LocalSessionFactoryBean

    spring集成hibernate由两种形式 1.继续使用Hibernate的映射文件*.hbm.xml 2.使用jpa形式的pojo对象, 去掉*.hbm.xml文件 一.继续使用Hibernate ...

  5. golang 自定义封包协议(转的)

    package protocol import ( "bytes" "encoding/binary" ) const ( ConstHeader = &quo ...

  6. Java中系统属性Properties介绍 System.getProperty()参数大全

       在JDK文档中System类中有这样的方法getProperties()在此方法的详细介绍中有下面的参数可供使用: java.version Java 运行时环境版本 java.vendor J ...

  7. asp.net分割字符串的几种方法

    在编写程序中,经常要用到分割的方法来处理一些字符串.这里总结了几种常用的分割方法:  1.最简单最常用的方法,以一个指定的字符进行的分割 string s="abcdeabcdeabcde& ...

  8. hdu 1312

    原题链接 题意:“@”为起点,“.”为路,求可以走的格子有多少个(包括起点) 水题 bfs搜一发 思路:只有可以走的节点才能进入队列,所以每次出队列时ans+1就可以了(没有退出条件,所有可进入的节点 ...

  9. VMwareTools 安装(VMware Player)

    1. VMare Tools 按钮是灰化的,需要对虚拟机做如下设置:(在虚拟机没有启动的状态下进行)     1)Player(P)->管理->虚拟机设置->CD/DVD  选择:使 ...

  10. 谷歌浏览器安装adblock广告屏蔽插件

    访问不到google的应用商店的话,就从网上直接查找adblock for chrome.打开浏览器的开发者模式,下载解压后直接拖拽到浏览器即可.下载地址http://www.cr173.com/so ...