HDU 2234 无题I(IDA)

无题I

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

int map[5][5];
int deep;

bool check_row() { //检查行
    int i;
    for(i = 1; i <= 4; i++)
        if(!(map[i][1] == map[i][2] && map[i][1] == map[i][3] && map[i][1] == map[i][4]))
            return 0;
    return 1;
}

bool check_col() { //检查列
    int i;
    for(i = 1; i <= 4; i++)
        if(!(map[1][i] == map[2][i] && map[1][i] == map[3][i] && map[1][i] == map[4][i]))
            return 0;
    return 1;
}

int get_h() { //IDA*的估测函数,这里是找到每行每列有多少个数字不在其位

    int s1 = 0, s2 = 0, i, j;

    for(i = 1; i <= 4; i++) {//每行扫描
        int a[10] = {0}, ans = 0;

        for(j = 1; j <= 4; j++)//固定行,扫描列
            a[map[i][j]]++;
        for(j = 1; j <= 4; j++)
            ans += a[j];

        s1 = max(s1, 4 - ans);
    }

    for(j = 1; j <= 4; j++) {//每列扫描
        int a[10] = {0}, ans = 0;

        for(i = 1; i <= 4; i++)//固定列,扫描行
            a[map[i][j]]++;
        for(i = 1; i <= 4; i++)
            ans += a[i];

        s2 = max(s2, 4 - ans);
    }

    return min(s1, s2);
}

void mov_l(int i) {
    int j;
    int t = map[i][1];
    for(j = 2; j <= 4; j++)
        map[i][j-1] = map[i][j];
    map[i][4] = t;
}

void mov_r(int i) {
    int j;
    int t = map[i][4];
    for(j = 4; j >= 2; j--)
        map[i][j] = map[i][j-1];
    map[i][1] = t;
}

void mov_u(int j) {
    int i;
    int t = map[1][j];
    for(i = 2; i <= 4; i++)
        map[i-1][j] = map[i][j];
    map[4][j] = t;
}

void mov_d(int j) {
    int i;
    int t = map[4][j];
    for(i = 4; i >= 2; i--)
        map[i][j] = map[i-1][j];
    map[1][j] = t;
}


int IDA(int step) {
    if(step == deep)
        return check_row() || check_col();

    //if(step + get_h() > deep)//估测剪枝, 不需要也能AC  
    //    return 0;

    int i;
    for(i = 1; i <= 4; i++) {
        mov_l(i);
        if(IDA(step+1))
            return 1;
        mov_r(i);//回溯

        mov_r(i);
        if(IDA(step+1))
            return 1;
        mov_l(i);//回溯
    }

    for(i = 1; i<=4; i++) {
        mov_u(i);
        if(IDA(step+1))
            return 1;
        mov_d(i);//回溯

        mov_d(i);
        if(IDA(step+1))
            return 1;
        mov_u(i);//回溯
    }

    return 0;
}

int main() {
    freopen("data.in", "r", stdin);
    int t, i, j;
    cin >> t;

    while(t--) {
        for(i = 1; i <= 4; i++)
            for(j = 1; j <= 4; j++)
                cin >> map[i][j];

        if(check_row() || check_col()) {
            cout << "0" << endl;
            continue;
        }

        deep = 1;
        for(; deep <= 5; ++deep) {
            if( IDA(0) )//每次都从0层开始
                break;
        }

        if(deep <= 5)
            cout << deep << endl;
        else
            cout << "-1" << endl;
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值