hdu 4801模拟题

/*
模拟;
注意:实质上一次魔方的一半要变化
用c++超内存
用g++过了
*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct node
{
    int f0[4],f4[4],f6[4],f8[4],f16[4],f20[4];
    int step;
} s,next;
int fnow[10000][6];
int kk(int f[4])
{
    if(f[0]==f[1]&&f[1]==f[2]&&f[2]==f[3])
        return 1;
    return 0;
}
int comp(struct node ss)//计算有几个面是同色
{
    int sum=0;
    sum+=kk(ss.f0);
    sum+=kk(ss.f4);
    sum+=kk(ss.f6);
    sum+=kk(ss.f8);
    sum+=kk(ss.f16);
    sum+=kk(ss.f20);
    return sum;
}
void  clock(int f[4],int fd[4])//顺时针旋转
{
    f[0]=fd[2];
    f[2]=fd[3];
    f[3]=fd[1];
    f[1]=fd[0];
    return ;
}
void clockfan(int f[4],int fd[4])//逆时针旋转
{
    f[0]=fd[1];
    f[1]=fd[3];
    f[3]=fd[2];
    f[2]=fd[0];
    return ;
}
void Switch(struct node s,int k)//6种旋转方式
{
    if(k==0)
    {
        next.f0[0]=s.f20[0];
        next.f0[2]=s.f20[2];
        next.f6[0]=s.f0[0];
        next.f6[2]=s.f0[2];
        next.f16[0]=s.f6[0];
        next.f16[2]=s.f6[2];
        next.f20[0]=s.f16[0];
        next.f20[2]=s.f16[2];
        clock(next.f4,s.f4);//要变化
    }
    if(k==1)
    {
        next.f20[0]=s.f0[0];
        next.f20[2]=s.f0[2];
        next.f0[0]=s.f6[0];
        next.f0[2]=s.f6[2];
        next.f6[0]=s.f16[0];
        next.f6[2]=s.f16[2];
        next.f16[0]=s.f20[0];
        next.f16[2]=s.f20[2];
        clockfan(next.f4,s.f4);//要变化
    }
    if(k==2)
    {
        next.f4[0]=s.f6[0];
        next.f4[1]=s.f6[1];
        next.f20[2]=s.f4[1];
        next.f20[3]=s.f4[0];
        next.f8[0]=s.f20[3];
        next.f8[1]=s.f20[2];
        next.f6[0]=s.f8[0];
        next.f6[1]=s.f8[1];
        clock(next.f0,s.f0);
    }
    if(k==3)
    {
        next.f6[0]=s.f4[0];
        next.f6[1]=s.f4[1];
        next.f4[0]=s.f20[3];
        next.f4[1]=s.f20[2];
        next.f20[3]=s.f8[0];
        next.f20[2]=s.f8[1];
        next.f8[0]=s.f6[0];
        next.f8[1]=s.f6[1];
        clockfan(next.f0,s.f0);
    }
    if(k==4)
    {
        next.f0[2]=s.f8[0];
        next.f0[3]=s.f8[2];
        next.f4[1]=s.f0[3];
        next.f4[3]=s.f0[2];
        next.f16[0]=s.f4[1];
        next.f16[1]=s.f4[3];
        next.f8[0]=s.f16[1];
        next.f8[2]=s.f16[0];
        clockfan(next.f6,s.f6);
    }
    if(k==5)
    {
        next.f8[0]=s.f0[2];
        next.f8[2]=s.f0[3];
        next.f0[3]=s.f4[1];
        next.f0[2]=s.f4[3];
        next.f4[1]=s.f16[0];
        next.f4[3]=s.f16[1];
        next.f16[1]=s.f8[0];
        next.f16[0]=s.f8[2];
        clock(next.f6,s.f6);
    }
}
int n,len;
void chh(int &f,int fd[4])
{
     f=fd[3];
     f=f*6+fd[2];
     f=f*6+fd[1];
     f=f*6+fd[0];
    return ;
}
void fswitch(struct node s,int kf[6])
{
    chh(kf[0],s.f0);
    chh(kf[1],s.f4);
    chh(kf[2],s.f6);
    chh(kf[3],s.f8);
    chh(kf[4],s.f16);
    chh(kf[5],s.f20);
    return ;
}
void bfs(struct node s)
{
    int j,k,maxx;
    struct node cur;
    maxx=comp(s);
    s.step=0;
    len=0;
    queue<node>q;
    q.push(s);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur.step>=n)continue;
        for(k=0; k<6; k++)
        {
            next=cur;
            Switch(cur,k);
            j=comp(next);
            next.step++;
            q.push(next);
            if(j>maxx)
                maxx=j;
                if(maxx==6)break;
        }
        if(maxx==6)break;
    }
    printf("%d
",maxx);
    return ;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        scanf("%d%d%d%d",&s.f0[0],&s.f0[1],&s.f0[2],&s.f0[3]);
        scanf("%d%d%d%d",&s.f4[0],&s.f4[1],&s.f6[0],&s.f6[1]);
        scanf("%d%d%d%d",&s.f8[0],&s.f8[1],&s.f4[2],&s.f4[3]);
        scanf("%d%d%d%d",&s.f6[2],&s.f6[3],&s.f8[2],&s.f8[3]);
        scanf("%d%d%d%d",&s.f16[0],&s.f16[1],&s.f16[2],&s.f16[3]);
        scanf("%d%d%d%d",&s.f20[0],&s.f20[1],&s.f20[2],&s.f20[3]);
        bfs(s);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410542.html