HDU 5012-Dice(BFS)

来源:互联网 发布:mac air 13寸 尺寸 编辑:程序博客网 时间:2024/04/29 12:53

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.
 

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.
 

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

                                                                     

题意:

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

思路:

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;}


0 0
原创粉丝点击