HDU 1667 The Rotation Game(IDA*)

The Rotation Game

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int map[10][10];
int _hash[8] = {3, 5, 3, 5, 5, 3, 5, 3};
char ans[100];
int step;

int get_h() {
    int i, num[10] = {0};
    for(i = 3; i <= 5; i++) {
        num[map[3][i]]++;
        num[map[5][i]]++;
    }
    num[map[4][3]]++;
    num[map[4][5]]++;
    return max(num[1], max(num[2], num[3]));//找出最大的
}

void turn_col(int k, int flag) {//flag = 1,向上移动 flag = 0,向下移动
    int i,tem;
    if(flag) {
        tem = map[1][k];
        for(i = 2; i<=7; i++)
            map[i-1][k] = map[i][k];
        map[7][k] = tem;
    } else {
        tem = map[7][k];
        for(i = 7; i>=2; i--)
            map[i][k] = map[i-1][k];
        map[1][k] = tem;
    }
}

void turn_row(int k, int flag) {//flag = 1,向左移动 flag = 0,向右移动
    int i,tem;
    if(!flag) {
        tem = map[k][1];
        for(i = 2; i<=7; i++)
            map[k][i-1] = map[k][i];
        map[k][7] = tem;
    } else {
        tem = map[k][7];
        for(i = 7; i>=2; i--)
            map[k][i] = map[k][i-1];
        map[k][1] = tem;
    }
}

int dfs(int cnt) {
    if(cnt > step) {//深度大于预期深度,返回
        return 0;
    }

    int tem = get_h(), i;

    if(tem == 8 && cnt == step)
        return 1;

    if(cnt + (8-tem) > step)//估计函数,剪枝
        return 0;

    char c;
    for(i = 0; i<8; i++) {
        c = i + 'A';
        ans[cnt] = c;
        if(c == 'A' || c == 'B') {
            turn_col(_hash[i], 1);

            if( dfs(cnt+1) )
                return 1;

            turn_col(_hash[i], 0);
        } else if(c == 'C' || c == 'D') {
            turn_row(_hash[i], 1);

            if( dfs(cnt+1) )
                return 1;

            turn_row(_hash[i], 0);
        } else if(c == 'E' || c == 'F') {
            turn_col(_hash[i], 0);

            if( dfs(cnt+1) )
                return 1;

            turn_col(_hash[i], 1);
        } else if(c == 'G' || c == 'H') {
            turn_row(_hash[i], 0);

            if( dfs(cnt+1) )
                return 1;

            turn_row(_hash[i], 1);
        }
    }

    return 0;

}

int main() {

    //freopen("data.in", "r", stdin);
    int a, i, j, k;

    while(~scanf("%d", &a), a) {//读取地图
        map[1][3] = a;
        scanf("%d", &a);
        map[1][5] = a;
        for(i = 2; i <= 7; i++) {
            if(i == 3 || i == 5) {
                for(j = 1; j <= 7; j++)
                    scanf("%d", &map[i][j]);
            } else
                scanf("%d%d", &map[i][3], &map[i][5]);
        }

        if(get_h() == 8) {
            printf("No moves needed\n%d\n", map[3][3]);//已经匹配就直接输出即可
            continue;
        }

        step = 1;
        while(1) {
            if( dfs(0) )
                break;
            step++;
        }
        ans[step] = '\0';
        printf("%s\n%d\n", ans, map[3][3]);
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值