HDU5012-Dice (BFS)

Source: Internet
Author: User

Question Link


Question: Here are two sides. If you can rotate the two sides to make them equal one by one (given in the title of the rotation rule), find the minimum number of steps. If not, output-1.

Idea: Use BFs to calculate the minimum number of steps.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXN = 6;struct node{    node() {        memset(arr, 0, sizeof(arr));        d = 0;     }    int arr[MAXN], d;}s, e;int vis[MAXN * 200000];int change(node a) {    int num = 0;    for (int i = 0; i < MAXN; i++) {        num = num * 10 + a.arr[i];     }    return num;}bool judge(node a, node b) {    for (int i = 0; i < MAXN; i++)        if (a.arr[i] != b.arr[i])            return false;    return true;}node turn(node a, int i) {    node c;    if (i == 1) {        c.arr[0] = a.arr[3];        c.arr[1] = a.arr[2];        c.arr[2] = a.arr[0];        c.arr[3] = a.arr[1];        c.arr[4] = a.arr[4];        c.arr[5] = a.arr[5];     }    if (i == 2) {        c.arr[0] = a.arr[2];        c.arr[1] = a.arr[3];        c.arr[2] = a.arr[1];        c.arr[3] = a.arr[0];         c.arr[4] = a.arr[4];        c.arr[5] = a.arr[5];     }    if (i == 3) {        c.arr[0] = a.arr[5];        c.arr[1] = a.arr[4];        c.arr[2] = a.arr[2];        c.arr[3] = a.arr[3];         c.arr[4] = a.arr[0];        c.arr[5] = a.arr[1];     }    if (i == 4) {        c.arr[0] = a.arr[4];        c.arr[1] = a.arr[5];        c.arr[2] = a.arr[2];        c.arr[3] = a.arr[3];         c.arr[4] = a.arr[1];        c.arr[5] = a.arr[0];    }    return c;}int bfs() {    memset(vis, 0, sizeof(vis));    queue<node> q;    q.push(s);    node tmp;     vis[change(s)] = 1;    while (!q.empty()) {        tmp = q.front();         q.pop();                    if (judge(tmp, e)) {            return tmp.d;        }        for (int i = 1; i <= 4; i++) {            node c;            c = turn(tmp, i);            if (!vis[change(c)]) {                c.d = tmp.d + 1;                 vis[change(c)] = 1;                q.push(c);            }         }    }     return -1;}int main() {    while (scanf("%d", &s.arr[0]) != EOF) {        for (int i = 1; i < MAXN; i++)             scanf("%d", &s.arr[i]);        for (int i = 0; i < MAXN; i++)             scanf("%d", &e.arr[i]);        printf("%d\n", bfs());    }        return 0;}


HDU5012-Dice (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.