您的位置:首页 > 其它

HDU 5012-Dice(BFS)

2014-09-15 22:24 423 查看
[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

0
3
-1

                                                                     

题意:

第一个筛子通过几次翻转得到第二个筛子。

思路:

BFS ,利用筛子的顺序得到六位数用vis数组去重。

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
const int d[4][6] = {{3, 2, 0, 1, 4, 5},
{2, 3, 1, 0, 4, 5},
{5, 4, 2, 3, 0, 1},
{4, 5, 2, 3, 1, 0}};
struct node
{
int a[10];
}c[30];
int ans;
int p[30], vis[666666];

int visit(node cc)
{
int s = 0;
for(int i = 0; i < 6; i ++){
s = s * 10 + cc.a[i];
}
return s;
}
int bfs()
{
int f = 0, r = 1;
memset(vis, 0, sizeof(vis));
p[0] = 0;
vis[visit(c[0])] = 1;
while(f < r){
if(visit(c[f]) == ans) return f;
for(int i = 0; i < 4; i ++){
for(int j = 0; j < 6; j ++){
c[r].a[j] = c[f].a[d[i][j]];
}
int ss = visit(c[r]);
if(!vis[ss]){
vis[ss] = 1;
p[r] = p[f] + 1;
r ++;
}
}
f ++;
}
return -1;
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%d%d%d %d%d%d", &c[0].a[0],&c[0].a[1],&c[0].a[2],
&c[0].a[3],&c[0].a[4],&c[0].a[5])){
int n;
ans = 0;
for(int i=0;i<6;i++){
scanf("%d", &n);
ans = ans*10 +n;
}
int ff = bfs();
if(ff == -1) printf("-1\n");
else printf("%d\n", p[ff]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU BFS