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 有疑问?点这里^_^
题目描述

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.
输入
输出
示例输入
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
提示
来源
示例程序
官方代码:
/**
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的更多相关文章
- POJ 1955 Rubik's Cube
暴力模拟就好了.... vim写代码真费事,手都写酸了... Rubik's Cube Time Limit: 1000MS Memory Limit: 30000K Total Submissi ...
- The Mathematics of the Rubik’s Cube
https://web.mit.edu/sp.268/www/rubik.pdf Introduction to Group Theory and Permutation Puzzles March ...
- 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 ...
- 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) ...
- 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 ...
- HDU5983Pocket Cube
Pocket Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tota ...
- Pocket Cube
Pocket Cube http://acm.hdu.edu.cn/showproblem.php?pid=5983 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 5292 Pocket Cube 结论题
Pocket Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5292 Description Pocket Cube is the 2×2× ...
- squee_spoon and his Cube VI---郑大校赛(求最长子串)
市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...
随机推荐
- 团队计划backlog
http://www.cnblogs.com/threemonkey/p/5388439.html
- Hibernate 一级缓存的陷阱
最近公司的应用经常报OOM,一开始我以为是公司业务数据太多,导致内存不够,所以只是简单的把容器的内存加大.撑了几天后这个错仍然被报出来.后来我仔 细分析过项目代码后,没有发现有任何引起内存泄漏的地方. ...
- ios面试(2015.10.30)
今天去深圳市丰泰瑞达实业有限公司面试,面试分为笔试和面试两部分,结果非常不理想,不过学到很多.面试题记录如下: 1.tableview的三个常用方法的实现 - (NSInteger)numberOfS ...
- JS中常遇到的浏览器兼容问题和解决方法【转】
今天整理了一下浏览器对JS的兼容问题,希望能给你们带来帮助,我没想到的地方请留言给我,我再加上: 常遇到的关于浏览器的宽高问题: //以下均可console.log()实验 var winW=docu ...
- mysql连接查询语句示例
eg: 内连接: select student.*,grade.* from student join grade where student.sid=grade.sid; selec ...
- HDU3732 背包DP
Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 网络地址转换NAT原理及其作用
1 概述 1.1 简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task F ...
- echart 扩展地图不显示问题
今天写项目需要一个安徽地图,但echart自带的安徽地图还是老版的,仍有巢湖市,但客户要求不能有,只好重新找, 后发现ECharts 地图数据在线生成工具 :http://ecomfe.github. ...
- 【HDU 3401 Trade】 单调队列优化dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题目大意:现在要你去炒股,给你每天的开盘价值,每股买入价值为ap,卖出价值为bp,每天最多买as ...
- CSS3部分新特性
1.旋转transform:rotate(30deg);-ms-transform:rotate(30deg); /* IE 9 */-moz-transform:rotate(30deg); /* ...