Hdu5012: Dice (BFS template)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5012

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. 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 ctions (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.

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 input1 2 3 4 5 61 2 4 5 61 2 4 5 61 2 5 61 2 5 6 4 31 2 4 5 61 4 2 5 3 6 sample Output03-1Source 2014 ACM/ICPC Asia Regional analysis of Xi'an online question: I think it is because I did not read the questions during the online competition. It was a geometric question. I thought I could not do it. Later I read more about it. I did not expect it to be a BFS template water question.
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <queue>using namespace std;int v[7][7][7][7][7][7];struct node{    int ff[7];    int ans;};struct node t,f;int a[7],b[7];int bfs(){    queue<node>q;    for(int i=1; i<=6; i++)        t.ff[i]=a[i];    t.ans=0;    q.push(t);    v[t.ff[1]][t.ff[2]][t.ff[3]][t.ff[4]][t.ff[5]][t.ff[6]]=1;    while(!q.empty())    {        t=q.front();        q.pop();        if(t.ff[1]==b[1]&&t.ff[2]==b[2]&&t.ff[3]==b[3]&&t.ff[4]==b[4]&&t.ff[5]==b[5]&&t.ff[6]==b[6])        {            printf("%d\n",t.ans);            return 1;        }        for(int i=1; i<=4; i++)        {            if(i==1)            {                f.ff[1]=t.ff[4];                f.ff[2]=t.ff[3];                f.ff[3]=t.ff[1];                f.ff[4]=t.ff[2];                f.ff[5]=t.ff[5];                f.ff[6]=t.ff[6];                if(v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]==0)                {                    f.ans=t.ans+1;                    v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]=1;                    q.push(f);                }            }            else if(i==2)            {                f.ff[1]=t.ff[3];                f.ff[2]=t.ff[4];                f.ff[3]=t.ff[2];                f.ff[4]=t.ff[1];                f.ff[5]=t.ff[5];                f.ff[6]=t.ff[6];                if(v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]==0)                {                    f.ans=t.ans+1;                    v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]=1;                    q.push(f);                }            }            else if(i==3)            {                f.ff[1]=t.ff[6];                f.ff[2]=t.ff[5];                f.ff[3]=t.ff[3];                f.ff[4]=t.ff[4];                f.ff[5]=t.ff[1];                f.ff[6]=t.ff[2];                if(v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]==0)                {                    f.ans=t.ans+1;                    v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]=1;                    q.push(f);                }            }            else if(i==4)            {                f.ff[1]=t.ff[5];                f.ff[2]=t.ff[6];                f.ff[3]=t.ff[3];                f.ff[4]=t.ff[4];                f.ff[5]=t.ff[2];                f.ff[6]=t.ff[1];                if(v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]==0)                {                    f.ans=t.ans+1;                    v[f.ff[1]][f.ff[2]][f.ff[3]][f.ff[4]][f.ff[5]][f.ff[6]]=1;                    q.push(f);                }            }        }    }    return 0;}int main(){    int F;    while(scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)    {        for(int i=1; i<=6; i++)            scanf("%d",&b[i]);        memset(v,0,sizeof(v));        F=bfs();        if(F==0) printf("-1\n");    }    return 0;}

 

 

Hdu5012: Dice (BFS template)

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.