HDU 5012 dice (BFS + memory-based search)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5012


Problem descriptionthere 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.
 
Inputthere 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.
Outputfor 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
 
Source2014 ACM/ICPC Asia Regional Xi 'an online


I posted a post to my teammates first. It will be supplemented later!

The Code is as follows:

#include <cstdio>#include <cstring>#include <queue>using namespace std;struct saizi{    int b1,b2,b3,b4,b5,b6;    int size;    saizi() {}    saizi(int a1,int a2,int a3,int a4,int a5,int a6,int si):b1(a1),b2(a2),b3(a3),b4(a4),b5(a5),b6(a6),size(si) {};    bool operator == (const saizi& b) const    {        if(b.b1==b1&&b.b2==b2&&b.b3==b3&&b.b4==b4&&b.b5==b5&&b.b6==b6)            return 1;        return 0;    }} sai,tem,tel;int dice[10][10][10][10][10][10];int a[10];int bfs(){    queue<saizi> q;    sai.size=0;    q.push(sai);    while(!q.empty())    {        sai=q.front();        q.pop();        if(sai==tel)            return sai.size;        if(dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]==1)            continue;        dice[sai.b1][sai.b2][sai.b3][sai.b4][sai.b5][sai.b6]=1;        tem=saizi(sai.b6,sai.b5,sai.b3,sai.b4,sai.b1,sai.b2,sai.size+1);        q.push(tem);        tem=saizi(sai.b5,sai.b6,sai.b3,sai.b4,sai.b2,sai.b1,sai.size+1);        q.push(tem);        tem=saizi(sai.b3,sai.b4,sai.b2,sai.b1,sai.b5,sai.b6,sai.size+1);        q.push(tem);        tem=saizi(sai.b4,sai.b3,sai.b1,sai.b2,sai.b5,sai.b6,sai.size+1);        q.push(tem);    }    return -1;}int main(){    int ans;    while(scanf("%d",&sai.b1)!=EOF)    {        memset(dice,0,sizeof(dice));        scanf("%d",&sai.b2);        scanf("%d",&sai.b3);        scanf("%d",&sai.b4);        scanf("%d",&sai.b5);        scanf("%d",&sai.b6);        scanf("%d",&tel.b1);        scanf("%d",&tel.b2);        scanf("%d",&tel.b3);        scanf("%d",&tel.b4);        scanf("%d",&tel.b5);        scanf("%d",&tel.b6);        ans=bfs();        printf("%d\n",ans);    }    return 0;}


The Code is as follows:

# Include <cstdio >#include <cstring> struct dice {int Top, bottom; int left, right; int front_f, back_ B; int step;} A [100017]; int goal [6]; int BFS () {int TP = 1, TL = 0; dice T, F; A [0]. step = 0; while (1) {T = A [TL ++]; If (T. top = goal [0] & T. bottom = goal [1] & T. left = goal [2] & T. right = goal [3] & T. front_f = goal [4] & T. back_ B = goal [5]) {return T. step; break;} else if (T. step> 4) {return T. step =-1; brea K;} else {f. top = T. front_f; // backward F. bottom = T. back_ B; F. left = T. left; F. right = T. right; F. front_f = T. bottom; F. back_ B = T. top; F. step = T. step + 1; A [TP ++] = f; F. top = T. back_ B; // forward F. bottom = T. front_f; F. left = T. left; F. right = T. right; F. front_f = T. top; F. back_ B = T. bottom; F. step = T. step + 1; A [TP ++] = f; F. top = T. right; // left F. bottom = T. left; F. left = T. top; F. right = T. bottom; F. front _ F = T. front_f; F. back_ B = T. back_ B; F. step = T. step + 1; A [TP ++] = f; F. top = T. left; // F. bottom = T. right; F. left = T. bottom; F. right = T. top; F. front_f = T. front_f; F. back_ B = T. back_ B; F. step = T. step + 1; A [TP ++] = f ;}} int main () {While (~ Scanf ("% d", & A [0]. top) {scanf ("% d", & A [0]. bottom); scanf ("% d", & A [0]. left); scanf ("% d", & A [0]. right); scanf ("% d", & A [0]. front_f); scanf ("% d", & A [0]. back_ B); For (INT I = 0; I <6; I ++) {scanf ("% d", & goal [I]);} int ans = BFS (); printf ("% d \ n", ANS);} return 0 ;}


HDU 5012 dice (BFS + memory-based search)

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.