首页 > 代码库 > HDU 5012 Dice (bfs + 记忆化搜索)
HDU 5012 Dice (bfs + 记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5012
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 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 5 6 4 3 1 2 3 4 5 6 1 4 2 5 3 6
Sample Output
0 3 -1
Source
2014 ACM/ICPC Asia Regional Xi‘an Online
先贴一发队友写的。等会再补上本弱的!
代码如下:
#include <cstdio> #include <cstring> #include <queue> using namespace std; struct saizi { int b1,b2,b3,b4,b5,b6; int size; saizi() {} saizi(int a1,int a2,int a3,int a4,int a5,int a6,int si):b1(a1),b2(a2),b3(a3),b4(a4),b5(a5),b6(a6),size(si) {}; bool operator == (const saizi& b) const { if(b.b1==b1&&b.b2==b2&&b.b3==b3&&b.b4==b4&&b.b5==b5&&b.b6==b6) return 1; return 0; } } sai,tem,tel; int dice[10][10][10][10][10][10]; int a[10]; int bfs() { queue<saizi> q; sai.size=0; q.push(sai); while(!q.empty()) { sai=q.front(); q.pop(); if(sai==tel) return sai.size; if(dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]==1) continue; dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]=1; tem=saizi(sai.b6,sai.b5,sai.b3,sai.b4,sai.b1,sai.b2,sai.size+1); q.push(tem); tem=saizi(sai.b5,sai.b6,sai.b3,sai.b4,sai.b2,sai.b1,sai.size+1); q.push(tem); tem=saizi(sai.b3,sai.b4,sai.b2,sai.b1,sai.b5,sai.b6,sai.size+1); q.push(tem); tem=saizi(sai.b4,sai.b3,sai.b1,sai.b2,sai.b5,sai.b6,sai.size+1); q.push(tem); } return -1; } int main() { int ans; while(scanf("%d",&sai.b1)!=EOF) { memset(dice,0,sizeof(dice)); scanf("%d",&sai.b2); scanf("%d",&sai.b3); scanf("%d",&sai.b4); scanf("%d",&sai.b5); scanf("%d",&sai.b6); scanf("%d",&tel.b1); scanf("%d",&tel.b2); scanf("%d",&tel.b3); scanf("%d",&tel.b4); scanf("%d",&tel.b5); scanf("%d",&tel.b6); ans=bfs(); printf("%d\n",ans); } return 0; }
代码如下:
#include <cstdio> #include <cstring> struct Dice { int top, bottom; int left, right; int front_f, back_b; int step; } a[100017]; int goal[6]; int bfs() { int tp = 1, tl = 0; Dice t, f; a[0].step = 0; while(1) { t = a[tl++]; if(t.top==goal[0]&&t.bottom==goal[1]&&t.left==goal[2]&&t.right==goal[3]&&t.front_f==goal[4]&&t.back_b==goal[5]) { return t.step; break; } else if(t.step > 4) { return t.step = -1; break; } else { f.top = t.front_f;//向后 f.bottom = t.back_b; f.left = t.left; f.right = t.right; f.front_f = t.bottom; f.back_b = t.top; f.step = t.step+1; a[tp++] = f; f.top = t.back_b;//向前 f.bottom = t.front_f; f.left = t.left; f.right = t.right; f.front_f = t.top; f.back_b = t.bottom; f.step = t.step+1; a[tp++] = f; f.top = t.right;//向左 f.bottom = t.left; f.left = t.top; f.right = t.bottom; f.front_f = t.front_f; f.back_b = t.back_b; f.step = t.step+1; a[tp++] = f; f.top = t.left;//向右 f.bottom = t.right; f.left = t.bottom; f.right = t.top; f.front_f = t.front_f; f.back_b = t.back_b; f.step = t.step+1; a[tp++] = f; } } } int main() { while(~scanf("%d",&a[0].top)) { scanf("%d",&a[0].bottom); scanf("%d",&a[0].left); scanf("%d",&a[0].right); scanf("%d",&a[0].front_f); scanf("%d",&a[0].back_b); for(int i = 0; i < 6; i++) { scanf("%d",&goal[i]); } int ans = bfs(); printf("%d\n",ans); } return 0; }
HDU 5012 Dice (bfs + 记忆化搜索)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。