Uva11212

转自https://www.cnblogs.com/collectionne/p/6792050.html
自己想了个新的加速策略,很棒

另外,这个blog写了个IDA*的基本框架https://blog.csdn.net/cFarmerReally/article/details/52124815

//Rey Uva11212  10ms
//没用刘汝佳的启发式策略1和2,我反而觉得很好
#include<bits/stdc++.h>

const int maxn = 9;

int n,a[maxn];

inline bool End()
{
    for(int i = 1; i < n; i++){
        if(a[i] <= a[i-1]) return false;
    }
    return true;
}

inline int h()
{
    int cnt = 0;
    for(int i = 1; i < n; i++)
        if(a[i] != a[i-1]+1) cnt++;
    return cnt;
}

int maxd;
const int intsz = sizeof(int);
const int asz = sizeof(a);
bool dfs(int d)
{
    if(3*d + h() > 3*maxd) return false;
    if(d==maxd && End()) return true;

    int old[maxn];//保存a
    memcpy(old,a,asz);
    int b[maxn];//剪切板

    for(int i = 0; i < n; i++) if( i == 0 || old[i] != old[i-1] + 1) //策略3 选择尽量长的连续片段 剪切的起点
    for(int j = i; j < n; j++) { //终点 和 策略3
        while(j+1 < n && old[j+1] == old[j] + 1)j++;//策略3
        memcpy(b,old+i,intsz*(j-i+1));
        //剪切移动片段
        for(int k = j+1;k < n;k++){//由于对称性,只要往后贴就行了         【这个加速策略真的很好!】
            while(k+1 < n && old[k+1] == old[k] + 1)k++;//策略3 不破坏
            memcpy(a+i,old+j+1,intsz*(k-j));
            memcpy(a+i+k-j,b,intsz*(j-i+1));
            if(dfs(d+1))return true;
            //恢复
            memcpy(a,old,asz);
        }
    }
    return false;
}

inline int solve()
{
    if(End())return 0;
    for(maxd = 1; maxd < 5 ;maxd++)
        if(dfs(0)) return maxd;
    return 5;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int Cas = 0;
    while(~scanf("%d",&n)&&n) {
        for(int i = 0; i < n; i++)
            scanf("%d",a+i);
        int ans = solve();
        printf("Case %d: %d\n",++Cas,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值