C++ 三阶魔方还原

                       

转载请注明出处,谢谢 http://blog.csdn.net/ACM_cxlove?viewmode=contents           by—cxlove
三阶魔方还原。因为只搜5层,所以使用IDA*搜索。由于每次旋转,每面中心颜色总不变,也就确定了最终的状态,找出每个面中与中间颜色不同的个数的最大值,其中每次旋转会更改每个面的3个位置的颜色,所以 就是(最大值+2)/3。
总共有12种旋转,找到其中的对应方式,使用转动数组就非常方便了,不过数组的初始化是相当蛋疼的工作,最好手头有个三阶魔方。顺时针与逆时针刚好对应。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;//表示每个面除中间的另外8个的位置short int cen[6][8]={    {0,1,2,3,5,6,7,8},{9,10,11,21,23,33,34,35},{12,13,14,24,26,36,37,38},    {15,16,17,27,29,39,40,41},{18,19,20,30,32,42,43,44},{45,46,47,48,50,51,52,53}};//转换数组,12种变换,两两对应,每次转换会更改20个位置short int change[12][20]={         {11,23,35,34,33,21,9,10,51,48,45,36,24,12,6,3,0,20,32,44},         {9,10,11,23,35,34,33,21,36,24,12,6,3,0,20,32,44,51,48,45},         {14,13,26,38,37,36,24,12,45,46,47,39,27,15,8,7,6,11,23,35},         {12,24,13,14,26,38,37,36,39,27,15,8,7,6,11,23,35,45,46,47},         {17,29,41,40,39,27,15,16,47,50,53,42,30,18,2,5,8,14,26,38},         {15,16,17,29,41,40,39,27,42,30,18,2,5,8,14,26,38,47,50,53},         {18,19,20,32,44,43,42,30,53,52,51,33,21,9,0,1,2,17,29,41},         {42,30,18,19,20,32,44,43,33,21,9,0,1,2,17,29,41,53,52,51},         {0,1,2,5,8,7,6,3,12,13,14,15,16,17,18,19,20,9,10,11},         {6,3,0,1,2,5,8,7,15,16,17,18,19,20,9,10,11,12,13,14},         {45,46,47,50,53,52,51,48,44,43,42,41,40,39,38,37,36,35,34,33},         {51,48,45,46,47,50,53,52,41,40,39,38,37,36,35,34,33,44,43,42}};char a[54];  //初始状态int depth;   //迭代加深搜索的层数bool flag;   //是否有解int centre[6]={4,22,25,28,31,49};//每个面中心坐标int get_h(char *maze){    int ret=0;    for(int i=0;i<6;i++){        int cnt=0;        for(int j=0;j<8;j++)            if(maze[cen[i][j]]!=maze[centre[i]])                cnt++;        ret=max(ret,cnt);    }    return (ret+2)/3;}int ans[10];//调试用的,输出当前的形状void debug(char *maze){    int k=0;    for(int i=0;i<3;i++){        printf("      ");        for(int j=0;j<3;j++)            printf("%c ",maze[k++]);        printf("\n");    }    for(int i=0;i<3;i++){        for(int j=0;j<12;j++)            printf("%c ",maze[k++]);        printf("\n");    }    for(int i=0;i<3;i++){        printf("      ");        for(int j=0;j<3;j++)            printf("%c ",maze[k++]);        printf("\n");    }}void IDAstar(int tmp_depth,char *b,int father){    if(flag)        return;    //A*剪枝    if(get_h(b)>tmp_depth)        return;    if(tmp_depth==0){        flag=true;        return;    }    for(int i=0;i<12;i++){        if(flag)            return;        if((i^father)==1)            continue;        char tmp[54];        memcpy(tmp,b,54*sizeof(char));        ans[tmp_depth]=i;        //转换        for(int j=0;j<20;j++)            tmp[change[i][j]]=b[change[i^1][j]];;        IDAstar(tmp_depth-1,tmp,i);    }}char get_in(){    char ch;    while(1){        ch=getchar();        if(ch>='a'&&ch<='z')            return ch;    }}int main(){    int t;    scanf("%d",&t);    while(t--){        for(int i=0;i<54;i++)            a[i]=get_in();        flag=false;        int Init=get_h(a);        if(Init==0){            printf("0\n");            continue;        }        for(depth=Init;depth<=5;depth++){            IDAstar(depth,a,-1);            if(flag){                printf("%d\n",depth);                printf("%d %d",ans[depth]/2,(ans[depth]&1)?-1:1);                for(int j=depth-1;j>0;j--)                    printf(" %d %d",ans[j]/2,(ans[j]&1)?-1:1);                printf("\n");                break;            }        }        if(!flag)            printf("-1\n");    }    return 0;}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/trigfj/article/details/86624260