hdu 5012 Dice 记忆化搜索

Dice

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


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)

hdu 5012 Dice 记忆化搜索_第1张图片
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

把骰子的初始状态记录下来。步数初始化为0;

这样不停得按他所说的四种转法记忆化+bfs就行了。 一般这种搜步数的我都用bfs,不知道dfs行不行。

中间在结构体中打的是括号的重载运算符操作。不知道为什么结构体中用数组不能用这个方法来构造。

size记录下步数,遇到重点状态,然后把size输出出来就好了。


#include<stdio.h>
#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){};
    /*saizi(int a1,int a2,int a3,int a4,int a5,int a6,int si)
    {
        b1=a1;
        a[2]=a2;
        a[3]=a3;
        a[4]=a4;
        a[5]=a5;
        a[6]=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()
{
    //if(dice[a[n][1]][a[n][2]][a[n][3]][a[n][4]][a[n][5]][a[n][6]])
    //return ;
    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;
         //左为轴
        //shun1    
        tem=saizi(sai.b6,sai.b5,sai.b3,sai.b4,sai.b1,sai.b2,sai.size+1);
        q.push(tem);
        
        //shun2
        //tem=saizi(sai.b2,sai.b1,sai.b3,sai.b4,sai.b6,sai.b5,sai.size+1);
        //q.push(tem);
        //shun3
        tem=saizi(sai.b5,sai.b6,sai.b3,sai.b4,sai.b2,sai.b1,sai.size+1);
        q.push(tem);
        


        //前为轴
        //shun1
        tem=saizi(sai.b3,sai.b4,sai.b2,sai.b1,sai.b5,sai.b6,sai.size+1);
        q.push(tem);
        
        //shun2
        //tem=saizi(sai.b2,sai.b1,sai.b4,sai.b3,sai.b5,sai.b6,sai.size+1);
        //q.push(tem);
        //shun3
        tem=saizi(sai.b4,sai.b3,sai.b1,sai.b2,sai.b5,sai.b6,sai.size+1);
        q.push(tem);
        


        //上为轴
        //顺1
        //tem=saizi(sai.b1,sai.b2,sai.b5,sai.b6,sai.b4,sai.b3,sai.size+1);
        //q.push(tem);
        /*
        //shun2
        tem=saizi(sai.b1,sai.b2,sai.b3,sai.b4,sai.b6,sai.b5,sai.size+1);
        q.push(tem);
        //shun3
        tem=saizi(sai.b1,sai.b2,sai.b6,sai.b5,sai.b3,sai.b4,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;
}






你可能感兴趣的:(C++,ACM,bfs,记忆化搜索)