搜索进阶------F - Magic Cube

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:

  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.

Input
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:
/—
| |
| 4 |
| |
/—±–±--±–
| | | | |
| 0 | 1 | 2 | 3 |
| | | | |
—±–±--±–/
| |
| 5 |
| |
—/

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

Output
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.

Sample Input
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

Sample Output
0
1
1 1

这道题首先看virtual judge上题的输入数据显示错了,迭代加深搜索,对深度进行搜索,
先把魔方每个单元编号,每个面会有两种转法,一共有12种转法,然后在搜索,12^5次的复杂度,先预处理出旋转所要变化单元的编号。。。
看别人博客才a的。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 54;

//将三阶魔方编号
//         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

int cent[7] = {23, 26, 29, 32, 5, 50};
int face[7][10] = {
{10,11,12,22,23,24,34,35,36},
{13,14,15,25,26,27,37,38,39},
{16,17,18,28,29,30,40,41,42},
{19,20,21,31,32,33,43,44,45},
{1,2,3,4,5,6,7,8,9},
{46,47,48,49,50,51,52,53,54}
};
int change[12][20] = {
                   {1,4,7,13,25,37,46,49,52,21,33,45,10,11,12,24,36,35,34,22},
                   {45,33,21,1,4,7,13,25,37,52,49,46,34,22,10,11,12,24,36,35},
                   {7,8,9,16,28,40,48,47,46,36,24,12,13,14,15,27,39,38,37,25},
                   {36,24,12,7,8,9,16,28,40,48,47,46,37,25,13,14,15,27,39,38},
                   {9,6,3,19,31,43,54,51,48,39,27,15,16,17,18,30,42,41,40,28},
                   {39,27,15,9,6,3,19,31,43,54,51,48,40,28,16,17,18,30,42,41},
                   {42,30,18,3,2,1,10,22,34,52,53,54,19,20,21,33,45,44,43,31},
                   {52,53,54,42,30,18,3,2,1,10,22,34,43,31,19,20,21,33,45,44},
                   {15,14,13,12,11,10,21,20,19,18,17,16,1,2,3,6,9,8,7,4},
                   {18,17,16,15,14,13,12,11,10,21,20,19,7,4,1,2,3,6,9,8},
                   {37,38,39,40,41,42,43,44,45,34,35,36,46,47,48,51,54,53,52,49},
                   {34,35,36,37,38,39,40,41,42,43,44,45,52,49,46,47,48,51,54,53}
                  } ;
char s[60];
int deep;
int a[8],b[8];

int judge()
{
    int sum = 0;
    for(int i = 0;i < 6;++i){
        for(int j = 0;j < 9;++j){
            if(s[face[i][j]] != s[cent[i]]) sum++;
        }
    }
    return (sum + 11) / 12;
}

bool dfs(int dept)
{
    int x = judge();
    if(x + dept > deep) return false;
    if(!x) return true;

    char tmp[60];
    memcpy(tmp,s,sizeof(s));
    for(int i = 0;i < 12;++i){
        for(int j = 0;j < 20;++j) s[change[i][j]] = tmp[change[i ^ 1][j]];
        a[dept] = i / 2;
        b[dept] = (i & 1) ? -1 : 1;
        if(dfs(dept + 1)) return true;
        memcpy(s,tmp,sizeof(tmp));
    }
    return false;
}

int main()
{
    //freopen("data.txt","r",stdin);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i = 1;i <= 54;++i){
            scanf(" %c",&s[i]);
        }
        int x = judge();
        if(x == 0) {
            printf("0\n");
            continue;
        }
        //迭代加深搜索
        bool flag = false;
        deep = 0;
        while(deep < 5){
            deep++;
            //cout << deep << endl;
            if(dfs(0)){
                flag = true;
                printf("%d\n",deep);
                for(int i = 0;i < deep;++i){
                    printf("%d %d\n",a[i],b[i]);
                }
                break;
            }
        }
        if(!flag){
            printf("-1\n");
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值