Magic Cube ZOJ - 2477

题目链接
题意:
给出魔方初始图,每次从6个面中选一个面进行旋转操作,求还原魔方所需要的最小步数,并输出每次操作和旋转方向,答案保证在5步以内
题目分析:
很恶心的一道题,题意5步一眼IDA* 启发函数也挺好想,找出每个面与中心颜色不同的数量, 每次旋转最多改变12个, 当不同数量/12>深度时剪枝,每次操作我们可以打表模拟6种操作时会导致的位置变化
不懂操作变化的可以点击
代码:

#include <bits/stdc++.h>
#define x first
#define y second
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define PI acos(-1)
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
const double eps = 1e-6;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
const int INF = 0x3f3f3f3f, mod = 1e9 + 7;
const int N = 100;
int ans[6], dir[6]; 
char a[N];
int e[6][10] = {
    {1, 2, 3, 4, 5, 6, 7, 8, 9},
    {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},
    {46, 47, 48, 49, 50, 51, 52, 53, 54}
};
int center[6] = {5, 23, 26, 29, 32, 50};
int change[12][20] = {
    {12, 24, 36, 35, 34, 22, 10, 11, 13, 25, 37, 46, 49, 52, 45, 33, 21, 1, 4, 7},
    {10, 11, 12, 24, 36, 35, 34, 22, 1, 4, 7, 13, 25, 37, 46, 49, 52, 45, 33, 21},

    {15, 27, 39, 38, 37, 25, 13, 14, 16, 28, 40, 48, 47, 46, 36, 24, 12, 7, 8, 9},
    {13, 14, 15, 27, 39, 38, 37, 25, 7, 8, 9, 16, 28, 40, 48, 47, 46, 36, 24, 12},
 
    {18, 30, 42, 41, 40, 28, 16, 17, 19, 31, 43, 54, 51, 48, 39, 27, 15, 9, 6, 3},
    {16, 17, 18, 30, 42, 41, 40, 28, 9, 6, 3, 19, 31, 43, 54, 51, 48, 39, 27, 15},
 
    {21, 33, 45, 44, 43, 31, 19, 20, 3, 2, 1, 10, 22, 34, 52, 53, 54, 42, 30, 18},
    {19, 20, 21, 33, 45, 44, 43, 31, 42, 30, 18, 3, 2, 1, 10, 22, 34, 52, 53, 54},
 
    {3, 6, 9, 8, 7, 4, 1, 2, 18, 17, 16, 15, 14, 13, 12, 11, 10, 21, 20, 19},
    {1, 2, 3, 6, 9, 8, 7, 4, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10},
 
    {48, 51, 54, 53, 52, 49, 46, 47, 40, 41, 42, 43, 44, 45, 34, 35, 36, 37, 38, 39},
    {46, 47, 48, 51, 54, 53, 52, 49, 37, 38, 39, 40, 41, 42, 43, 44, 45, 34, 35, 36}
};
int f(){
    int sum = 0;
    for (int i = 0; i < 6; i ++)
        for (int j = 0; j < 9; j ++)
            if (a[e[i][j]] != a[center[i]])
                sum ++;
    return sum;
}
bool dfs(int u, int max_depth){
    int x = f();
    if (u + ceil(x / 12.0) > max_depth) return false;
    if (x == 0) return true;
    char t[100];
    for (int i = 0; i < 12; i ++){
        memcpy(t, a, sizeof(a));
        for (int j = 0; j < 20; j ++)
            a[change[i][j]] = t[change[i ^ 1][j]];//如果i为偶数+1,i为奇数减一
        ans[u] = i / 2;
        if (!(i & 1)) dir[u] = 1;
        else dir[u] = -1;
        if (dfs(u + 1, max_depth)) return true;
        memcpy(a, t, sizeof(t));
    }
    return false;
}
signed main(){
    int T;
    cin >> T;
    while (T--){
        for (int i = 1; i <= 54; i ++) cin >> a[i];
        if(!f()){
            cout << 0 << endl;
            continue;
        }
        int depth = 0;
        while (!dfs(0, depth) && depth <= 5) depth ++;
        if (depth > 5){
            cout << "-1" << endl;
            continue;
        }
        cout << depth << endl;
        for (int i = 0; i < depth; i ++)
            cout << ans[i] << ' ' << dir[i] << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值