Dice(BFS)

Dice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1109    Accepted Submission(s): 584


Problem Description
There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a 1.a 2,a 3,a 4,a 5,a 6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b 1.b 2,b 3,b 4,b 5,b 6 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 a i ≠ a j and b i ≠ b j 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, a i ≠ b i). Ddy wants to make the two dices look the same from all directions(which means for all i, a i = b i) only by the following four rotation operations.(Please read the picture for more information)

Dice(BFS)
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 a 1,a 2,a 3,a 4,a 5,a 6, representing the numbers on dice A.

The second line consists of six integers b 1,b 2,b 3,b 4,b 5,b 6, 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

 

      题意:

      给出两个骰子 A 和 B,分别给出六个面的数字,有向左转,向右转,向前转,向后转四种操作,问最少几步能转到相同,输出步数,如果无论如何都不可能则输出 -1。

 

      思路:

      BFS。表示出 4 种转的状态即可,白痴错误 WA 了两次。

 

       AC:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

typedef struct {
    int ans[10];
    int step, l_t, r_t, b_t, f_t;
} state;

int fin[10], num[10];

void Left (int *a) {
    int b[10];
    for (int i = 1; i <= 6; ++i) {
        b[i] = a[i];
    }
    a[1] = b[4], a[2] = b[3], a[3] = b[1];
    a[4] = b[2], a[5] = b[5], a[6] = b[6];
}

void Right (int *a) {
    int b[10];
    for (int i = 1; i <= 6; ++i) {
        b[i] = a[i];
    }
    a[1] = b[3], a[2] = b[4], a[3] = b[2];
    a[4] = b[1], a[5] = b[5], a[6] = b[6];
}

void Front (int *a) {
    int b[10];
    for (int i = 1; i <= 6; ++i) {
        b[i] = a[i];
    }
    a[1] = b[6], a[2] = b[5], a[3] = b[3];
    a[4] = b[4], a[5] = b[1], a[6] = b[2];
}

void Back (int *a) {
    int b[10];
    for (int i = 1; i <= 6; ++i) {
        b[i] = a[i];
    }
    a[1] = b[5], a[2] = b[6], a[3] = b[3];
    a[4] = b[4], a[5] = b[2], a[6] = b[1];
}

bool test1 () {
    int tt[10][10];

    memset(tt, 0, sizeof(tt));
    for (int i = 2; i <= 6; i += 2) {
        tt[ num[i - 1] ][ num[i] ] = 1;
        tt[ num[i] ][ num[i - 1] ] = 1;
    }

    for (int i = 2; i <= 6; i += 2) {
        if (!tt[ fin[i - 1] ][ fin[i] ]) return false;
        if (!tt[ fin[i] ][ fin[i - 1] ]) return false;
    }

    return true;
}

bool test2 (int *a) {
    for (int i = 1; i <= 6; ++i) {
        if (a[i] != fin[i]) return false;
    }

    return true;
}

int bfs () {
    state a;
    for (int i = 1; i <= 6; ++i) {
        a.ans[i] = num[i];
    }
    a.l_t = a.r_t = a.f_t = a.b_t = 0;
    a.step = 0;

    queue<state> q;
    q.push(a);

    while (!q.empty()) {

        state now = q.front(); q.pop();

        if (test2(now.ans)) return now.step;

        state b;
        b.step = now.step + 1;

        if (now.l_t < 3) {
            for (int i = 1; i <= 6; ++i) {
                b.ans[i] = now.ans[i];
            }
            Left(b.ans);
            b.b_t = now.b_t;
            b.f_t = now.f_t;
            b.r_t = now.r_t;
            b.l_t = now.l_t + 1;
            q.push(b);
        }

        if (now.r_t < 3) {
            for (int i = 1; i <= 6; ++i) {
                b.ans[i] = now.ans[i];
            }
            Right(b.ans);
            b.l_t = now.l_t;
            b.b_t = now.b_t;
            b.f_t = now.f_t;
            b.r_t = now.r_t + 1;
            q.push(b);
        }

        if (now.f_t < 3) {
            for (int i = 1; i <= 6; ++i) {
                b.ans[i] = now.ans[i];
            }
            Front(b.ans);
            b.b_t = now.b_t;
            b.l_t = now.l_t;
            b.r_t = now.r_t;
            b.f_t = now.f_t + 1;
            q.push(b);
        }

        if (now.b_t < 3) {
            for (int i = 1; i <= 6; ++i) {
                b.ans[i] = now.ans[i];
            }
            Back(b.ans);
            b.l_t = now.l_t;
            b.f_t = now.f_t;
            b.r_t = now.r_t;
            b.b_t = now.b_t + 1;
            q.push(b);
        }
    }

    return -1;
}

int main() {

    while (~scanf("%d", &num[1])) {

        for (int i = 2; i <= 6; ++i)
            scanf("%d", &num[i]);

        for (int i = 1; i <= 6; ++i)
            scanf("%d", &fin[i]);

        if (!test1()) printf("-1\n");
        else printf("%d\n", bfs());

    }

    return 0;
}

 

 

 

你可能感兴趣的:(bfs)