Magic Cube

传送门ZOJ2477

描述

This is a very popular game for children. In this game, there’s a cube, which consists of 3 3 3 small cubes. We can unwrap the cube, it will become like this:

1
2
3
4
5
6
7
8
9
      w w w
      w w w
      w w w
r r r g g g b b b o o o
r r r g g g b b b o o o
r r r g g g b b b o o o
      y y y
      y y y
      y y y

The letters means the color on the small cubes. For example, ‘r’ means red, ‘g’ means green, ‘y’ means yellow….The goal for this game is to rotate the faces of the cube to make each of the faces contains only one color. Note there’re exact 6 kind of colors on the cube and there’re exact 9 small rectangles totally in any time in the game.
Do you know how to rotate the faces? I think most of you have known it. But I would like to show it again. When a face is rotated, the configuration of colors in all the adjacent faces changes. For the cube above, after we rotate the green face clock-wise, the last line of ‘w’ face will become the left column of ‘b’ face, the left column of ‘b’ face will become the top line of ‘y’ face, etc. As you may know, reaching the final position from a scrambled configuration can be quite challenging.
In this problem, you are given a configuration of the cube, and asked to give a way to reach the final position. To reduce the difficulty, the steps required will never be greater than 5.

输入

The input contains an integer in the first line, which indicates the number of the test cases. In each test case, there’re exact 10 lines. The first line is an empty line. The next 9 lines contain a configuration. The format can be seen in the sample input. For simplicity, we give an index to each face as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
    /---\
    |   |
    | 4 |
    |   |
/---+---+---+---\
|   |   |   |   |
| 0 | 1 | 2 | 3 |
|   |   |   |   |
\---+---+---+---/
    |   |
    | 5 |
    |   |
    \---/

Note that there’s a space between two adjacent letters.

输出

For each test case, the first line of the output is the smallest count N of the steps to reach the winning position. If the winning position can’t be reached in 5 steps, print -1 in this line. Otherwise print each step in one line in the following N lines. A step contains two integers, the first one means the face index, and the second one means the direction. 1 means clock-wise and -1 means counter clock-wise. If the given position is the winning position, print 0 for such test case simply. If there’re multiple solutions, any one is acceptable.

样例

  • Input

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    2
          w w w
          w w w
          w w w
    r r r g g g b b b o o o
    r r r g g g b b b o o o
    r r r g g g b b b o o o
          y y y
          y y y
          y y y
    
          w w w
          w w w
          b b b
    r r w g g g y b b o o o
    r r w g g g y b b o o o
    r r w g g g y b b o o o
          r r r
          y y y
          y y y
    
  • Output
    0
    1
    1 1

题解

  • 题意:给出魔方六面,每一面标号如上,给出一种排列,你需要把它复原,求最少步数和步骤,不能超过5步。1表示顺时针,-1表示逆时针。
  • 迭代深搜,deep从1到5。贼恶心一题,详细步骤看代码

    Code

    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
    
    #include<bits/stdc++.h>
    #define INIT(a,b) memset(a,b,sizeof(a))
    #define LL long long
    using namespace std;
    const int inf=0x3f3f3f3f;
    const int N=1e6+7;
    const int mod=1e9+7;
    int cube[6][9] = {
    	{4,0,1,2,3,5,6,7,8},{22,9,10,11,21,23,33,34,35},{25,12,13,14,24,26,36,37,38},
        {28,15,16,17,27,29,39,40,41},{31,18,19,20,30,32,42,43,44},{49,45,46,47,48,50,51,52,53}
    };
    //只有20个需要改变,每个中心无需改变,转动时从i行变成i^1行
    const int rot[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}
    };
    int deep=0,ans[10][2];//ans[i][0]表示深度为i时转动的面,ans[i][1]表示转动方向
    char a[60];
    bool judge(){
        for(int i=0;i<6;i++){
            for(int j=0;j<8;j++){
                if(a[cube[i][j]]!=a[cube[i][j+1]])
                    return false;
            }
        }
        return true;
    }
    void change(int k){
        int t=k^1;
        char tem[60];
        memcpy(tem,a,sizeof(a));
        for(int i=0;i<20;i++)
            a[rot[k][i]]=tem[rot[t][i]];
    }
    bool dfs(int t){
        if(t>deep) return false;
        if(judge()) return true;
        char tem[60];
        memcpy(tem,a,sizeof(a));
        for(int i=0;i<12;i++){
            change(i);
            ans[t][0]=i/2;
            ans[t][1]=i&1 ? -1:1;
            if(dfs(t+1))return true;
            memcpy(a,tem,sizeof(tem));
        }
        return false;
    }
    int main(){
    	int t;
    	scanf("%d",&t);
    	for(int cas=0;cas<t;cas++){
            if(!cas) getchar();
            for(int i=0;i<54;i++){
                while(1){
                    a[i]=getchar();
                    if(a[i]>='a'&&a[i]<='z') break;
                }
            }
            for(deep=0;deep<=6;deep++){
                if(deep==6) {
                    printf("-1\n");
                    break;
                }
                if(dfs(0)) {
                    printf("%d\n",deep);
                    for(int i=0;i<deep;i++)
                        printf("%d %d\n",ans[i][0],ans[i][1]);
                    break;
                }
            }
    
    	}
    
    	return 0;
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值