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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章