UVA 1343 - The Rotation Game

参考的刘汝佳大神的思路 IDA*算法  感觉用bfs会有超内存的危险==

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int maxn = 25;
const int id[10][10] =
    { {0, 2, 6, 11, 15, 20, 22},
      {1, 3, 8, 12, 17, 21, 23},
      {10, 9, 8, 7, 6, 5, 4 },
      {19, 18, 17, 16, 15, 14, 13},
      {23, 21, 17, 12, 8, 3, 1},
      {22, 20, 15, 11, 6, 2, 0},
      {13, 14, 15, 16, 17, 18, 19},
      {4, 5, 6, 7, 8, 9, 10} };
const int cen[10] = {6, 7, 8, 11, 12, 15, 16, 17};
const int rev[8] = {5, 4, 7, 6, 1, 0, 3, 2};
char ans[10000];
int str[maxn];
int dep;

bool judge()
{
    for(int i = 0; i < 8; ++i)
        if(str[cen[i]] != str[cen[0]]) return false;
    return true;
}

void move(int i) {
    int t = str[id[i][0]];
    for(int j = 0; j < 6; j++) str[id[i][j]] = str[id[i][j+1]];
    str[id[i][6]] = t;
}

int diff(int x) {
    int res = 0;
    for(int i = 0; i < 8; ++i) if(str[cen[i]] != x) ++res;
    return res;
}

int q() {
    return min(diff(1), min(diff(2), diff(3)));
}

bool dfs(int d) {
    if(judge()) {
        //cout << "++" << endl;
        ans[d] = '\0';
        printf("%s\n", ans);
        return true;
    }
    if(d + q() > dep) return false;

    for(int i = 0; i < 8; ++i) {
        char t = 'A' + i;
        ans[d] = t;
        move(i);
        if(dfs(d+1)) return true;
        move(rev[i]);
    }
    return false;
}

int main()
{
    while(scanf("%d", &str[0]) != EOF && str[0]) {
        for(int i = 1; i < 24; ++i) scanf("%d", &str[i]);
        memset(ans, 0, sizeof(ans));
        if(judge()) printf("No moves needed\n");
        else {
            dep = 0;
            while(!dfs(0)) ++dep;
        }
        //printf("%d\n", dep);
//        for(int i = 0; i < 24; ++i) printf("%d ", str[i]);
//        printf("\n");
        printf("%d\n", str[7]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值