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 the wisest man. When Liao was ninety-nine years old, the Zhu appeared to him and said, "I am God Almighty; walk before me and be blameless.
I will confirm my covenant between me and you and will greatly increase your wisdom." At that time, Liao has a problem:
Liao has a Rubik's Cube and the surfaces of it are drawn as black or white. He can rotate it as the rules of the Rubik's Cube. Liao asked Zhu:"how many states the Rubik's Cube can reach?"
Zhu is omniscient and omnipotent, but he did not tell the answer. Can you tell the poor man the answer? Remember two states are regarded as the same one if you change views and find the two states is the same one.
For those of you unfamiliar with the puzzle, a Rubik's Cube, comes in the form of a cube where each face is divided into three rows and three columns (nine "squares". Any of the six faces of the cube may be rotated either clockwise or counterclockwise, which also rotates the three nearest squares on each adjoining face onto a new face, respectively.
Input
Intput contains multiple test cases.
The first line is an integer 1≤T≤10, the number of test cases.
Each case begins with an integer n, indicating the number of the squares of blacks.
The next line contains n integers a1,a2,..,an, representing squares indexed as a1,a2,..,an are black respectively. All ai are defferent. The indexes of squares are showed in the pictures. (0≤n≤5,0≤ai≤53).
Output
For each test case,output a single line "Case #x: y", where x is the case number, starting from 1. And y is the answer.
Sample Input
3
2
0 47
2
24 53
2
36 46
Sample Output
Case #1: 15
Case #2: 15
Case #3: 24
Hint
题意
给你一个魔方,告诉你这个魔方有一些格子是黑色的,问你这个魔方旋转之后能够得到多少个不同的魔方。
就是不同的要,完全不同。
就旋转相同,也算相同。
题解:
XJB搜,你需要买一个魔方,然后XJB转一下 ,看看转移是什么样的,当然你有强大的脑补能力也可以……
然后就完了……
代码
//by Atlantis67
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int f[4][54]={
{2,5,8,1,4,7,0,3,6,
36,37,38,12,13,14,15,16,17,
9,10,11,21,22,23,24,25,26,
18,19,20,30,31,32,33,34,35,
27,28,29,39,40,41,42,43,44,
45,46,47,48,49,50,51,52,53},
{6,3,0,7,4,1,8,5,2,
18,19,20,21,22,23,24,25,26,
27,28,29,30,31,32,33,34,35,
36,37,38,39,40,41,42,43,44,
9,10,11,12,13,14,15,16,17,
47,50,53,46,49,52,45,48,51},
{29,32,35,28,31,34,27,30,33,
2,5,8,1,4,7,0,3,6,
20,23,26,19,22,25,18,21,24,
47,50,53,46,49,52,45,48,51,
42,39,36,43,40,37,44,41,38,
11,14,17,10,13,16,9,12,15},
{18,19,20,21,22,23,24,25,26,
11,14,17,10,13,16,9,12,15,
45,46,47,48,49,50,51,52,53,
33,30,27,34,31,28,35,32,29,
8,7,6,5,4,3,2,1,0,
44,43,42,41,40,39,38,37,36}
};
set<LL> s;
LL duilie[10000000];
int tot=0;
void pri(LL a,LL b,int ha)
{
printf("================\n");
for (int j=0;j<54;j++) if ((a>>j)&1) printf("%d ",j);puts("");
for (int j=0;j<54;j++) if ((b>>j)&1) printf("%d ",j);puts("");
printf("%d\n",ha);
getchar();
}
void BFS(LL first)
{
int fron=0,rear=1;
duilie[0]=first;
s.insert(first);
while (fron<rear){
LL now=duilie[fron];
for (int k=0;k<4;k++){
LL next=0;
for (int j=0;j<54;j++) {
if ((now>>j)&1)
next|=(1LL<<f[k][j]);
}
if (s.find(next)==s.end()) {
//pri(now,next,k);
duilie[rear++]=next;
s.insert(next);
}
}
fron++;
}
tot=rear;
}
LL w[100];
set<LL> si;
int wtot;
void gao(LL first)
{
si.clear();wtot=0;
int fron=0,rear=1;
w[0]=first;
si.insert(first);
while (fron<rear){
LL now=w[fron];
for (int k=1;k<4;k++){
LL next=0;
for (int j=0;j<54;j++) {
if ((now>>j)&1)
next|=(1LL<<f[k][j]);
}
if (si.find(next)==si.end()) {
w[rear++]=next;
si.insert(next);
}
}
fron++;
}
wtot=rear;
}
int suan()
{
int ans=0;
for (int j=0;j<tot;j++){
if (s.find(duilie[j])!=s.end()){
gao(duilie[j]);
for (int k=0;k<wtot;k++) s.erase(w[k]);
assert(24%wtot==0);
ans++;
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
int t=T;
while (T--){
int n;
scanf("%d",&n);
LL first=0;
for (int j=0;j<n;j++){
int g;
scanf("%d",&g);
first|=(1LL<<g);
}
//cout<<first<<endl;
s.clear();tot=0;
BFS(first);
printf("Case #%d: %d\n",t-T,suan());
}
return 0;
}
HDU 5836 Rubik's Cube BFS的更多相关文章
- hdu 3309 Roll The Cube ( bfs )
Roll The Cube Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- sdutoj 2606 Rubik’s cube
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2606 Rubik’s cube Time Li ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
- 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 5876 关于补图的bfs
1.HDU 5876 Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU(1175),连连看,BFS
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
随机推荐
- bzoj千题计划186:bzoj1048: [HAOI2007]分割矩阵
http://www.lydsy.com/JudgeOnline/problem.php?id=1048 #include<cmath> #include<cstdio> #i ...
- SQL语句(十三)多表查询
多表查询 1. 笛卡尔乘积 简单格式 SELECT * 或字段列表 FROM 数据表列表 实例 --1. 笛卡尔乘积 (五条件的连接--很多条件无意义) Select * from Student, ...
- java学习路线-从入门到入土
以下是个人学习路线,资源等我找到了 share,如果没找到请自行百度: 1.javase 观看 毕向东的 javase ,主要是老毕口才略屌,听着不容易打瞌睡,冷不丁吓你一大跳 老毕的年代久远,我已经 ...
- 用phpStorm的数据库工具来管理你的数据库
phpStorm是一个功能强大的IDE,不仅对PHP提供了支持,而且对前端HTML.CSS.JavaScript的支持也是非常不错的.此外,phpStorm还集成了很多实用的功能,下面就phpStor ...
- 20165320 2017-2018-2《Java程序设计》课程总结
20165320 2017-2017-2<Java程序设计>课程总结 一.每周作业链接汇总 1.我期待的师生关系 20165320 我期望的师生关系 2.学习基础和C语言基础调查 2016 ...
- 移动端点击300ms延迟
转载自:http://www.jianshu.com/p/6e2b68a93c88 一.移动端300ms点击延迟 一般情况下,如果没有经过特殊处理,移动端浏览器在派发点击事件的时候,通常会出现300m ...
- 补充NTP知识的初中高
前言 网上流传阿里穆工对NTP知识梳理的初级和中级版本.我从时钟服务器厂商在实践中的经验对穆工的文档进行再次整理和补充,希望对使用此设备的客户和对此有兴趣的同学给出一些指引. 个人认为对知识的了解应该 ...
- Java @Override 注解
@Override注解,不是关键字,但可以当关键字使用,可以选择添加这个注解,在你不留心重载而并非复写了该方法时,编译器就会产生一条错误:The method doh(Milhouse) of typ ...
- SOA 设计的 9 大原则
面向服务的架构 (SOA) 设计要尽可能地简单.在设计一个 SOA 服务的时候要谨记这 9 大设计原则: 1. 标准服务契约 服务要遵循一个服务描述. 2. 松耦合 服务之间的依赖最小化. 3. 服务 ...
- USING NHIBERNATE WITH MySQL
In previous USING NHIBERNATE WITH SQLITE, we connect SQLITE with ORM framework NHibernate. One of th ...