hdu 5012 Dice(bfs)
来源:互联网 发布:用c语言编写99乘法表 编辑:程序博客网 时间:2024/05/16 05:57
Dice
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 577 Accepted Submission(s): 335
Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6 to be numbers on specific faces of dice B. It’s guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.
At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)
Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)
Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.
Input
There are multiple test cases. Please process till EOF.
For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.
The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.
For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.
The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.
Output
For each test case, print a line with a number representing the answer. If there’s no way to make two dices exactly the same, output -1.
Sample Input
1 2 3 4 5 61 2 3 4 5 61 2 3 4 5 61 2 5 6 4 31 2 3 4 5 61 4 2 5 3 6
Sample Output
03-1
用队列进行广搜就行了,开一个6维数组。
#include"stdio.h"#include"string.h"#include"queue"#include"iostream"#include"algorithm"using namespace std;#define N 7#define LL __int64int p[N][N][N][N][N][N];int a[N],b[N];struct node{ int c[N],t; friend bool operator<(node a,node b) { return a.t>b.t; }};int bfs(){ int i,flag=-1; memset(p,0,sizeof(p)); priority_queue<node>q; node cur,next; for(i=1;i<N;i++) cur.c[i]=a[i]; cur.t=0; q.push(cur); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; while(!q.empty()) { cur=q.top(); q.pop(); for(i=1;i<N;i++) if(b[i]!=cur.c[i]) break; if(i==N) { flag=cur.t; break; } next.t=cur.t+1; next.c[1]=cur.c[4]; next.c[2]=cur.c[3]; next.c[3]=cur.c[1]; next.c[4]=cur.c[2]; next.c[5]=cur.c[5]; next.c[6]=cur.c[6]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } next.c[1]=cur.c[3]; next.c[2]=cur.c[4]; next.c[3]=cur.c[2]; next.c[4]=cur.c[1]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } next.c[1]=cur.c[6]; next.c[2]=cur.c[5]; next.c[3]=cur.c[3]; next.c[4]=cur.c[4]; next.c[5]=cur.c[1]; next.c[6]=cur.c[2]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } next.c[1]=cur.c[5]; next.c[2]=cur.c[6]; next.c[5]=cur.c[2]; next.c[6]=cur.c[1]; for(i=1;i<N;i++) a[i]=next.c[i]; if(p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]==0) { q.push(next); p[a[1]][a[2]][a[3]][a[4]][a[5]][a[6]]=1; } } return flag;}int main(){ int i; while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=-1) { for(i=1;i<N;i++) scanf("%d",&b[i]); int ans=bfs(); printf("%d\n",ans); } return 0;}
0 0
- HDU 5012 Dice (bfs)
- HDU - 5012 Dice(BFS)
- HDU-#5012 Dice(BFS)
- hdu 5012 Dice(bfs)
- HDU 5012 Dice (BFS)
- HDU 5012-Dice(BFS)
- HDU 5012 Dice bfs
- HDU - 5012 Dice BFS
- bfs-HDU 5012Dice
- HDU - 5012 Dice(bfs+hash)
- HDU 5012 Dice 普通bfs
- HDU 5012 Dice (bfs + 记忆化搜索)
- BFS + 剪枝 之 hdu 5012 Dice
- hdu HDU5012-Dice(BFS)
- hdu 5012 Dice(西安网络赛F题,BFS)
- hdu 5012 Dice
- HDU 5012 Dice
- hdu 5012 dice
- solr schema.xml
- 权限管理设计一
- C语言中的柔性数组
- 转载和积累系列 - MapReduce原理
- 对于“不返回任何键列信息的 SelectCommand 不支持 UpdateCommand 的动态 SQL 生成”问题的解决
- hdu 5012 Dice(bfs)
- 你既然离不开她就应该和你老婆离婚
- 转:Objective-C Message
- [UI] hight=0或者width=0配合weight的使用
- Json文件 使用rapidjson获取Json数据
- 深圳利民网P2P借贷服务平台简介
- 八大理由告诉你,微信电商为什么走不通
- apt-get moo
- 可是我老婆跟了我这么多年了我又于心不忍