poj3460 ida*

估价函数很经典,每个值的后继值错误的个数除以3。因为一次操作最多改变3个值的后继值。

1A,IDA*已经写得很纯熟了。

#include <iostream>
#include <cmath>
using namespace std;
int a[25],n,bound,cas;
int ans;
int h(int *a) {
    int hv = 0;
    if (a[1]!=1) hv++;
    for (int i=1;i<n;i++)
        if (a[i+1]!=a[i]+1) hv++;
    return (hv+2)/3;
}
void trans(int *a,int p1,int p2,int p3) {
     int tmp[20];
     for (int i=p1;i<=p2;i++)
         tmp[i]=a[i];
     for (int i=p2+1;i<=p3;i++)
         a[i-p2+p1-1]=a[i];
     for (int i=p1+p3-p2;i<=p3;i++)
         a[i]=tmp[i+p2-p3];
}
int dfs(int *a,int dep) {
    int hv = h(a);
    //printf("%d %d\n",dep,hv);
    if (dep>=5) {
                ans = 2;
                return 5;
    }
    if (hv + dep > bound) return hv+dep;
    if (hv==0) {
               ans = 1;
               return dep;
    }
    int next_bound = 10000;
    for (int i=1;i<n;i++)
        for (int j=i+1;j<=n;j++)
            for (int k=i;k<j;k++) {
                int tmp[20];
                for (int r=1;r<=n;r++) tmp[r]=a[r];
                trans(tmp,i,k,j);
                int new_bound = dfs(tmp,dep+1);
                if (ans!=0) return new_bound;
                next_bound = min(next_bound,new_bound);
            }
    return next_bound;
}
void ida_star() {
     bound = h(a);
     ans = 0;
     while (!ans && bound<=10)
           bound = dfs(a,0);
     if (ans==1) printf("%d\n",bound);
     else printf("5 or more\n");
}
int main() {
    scanf("%d",&cas);
    for (int tt=1;tt<=cas;tt++) {
          scanf("%d",&n);
          for (int i=1;i<=n;i++)
              scanf("%d",&a[i]);
                  
          if (h(a)==0) {
                    printf("0\n");
                    continue;
          }
          ida_star();
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值