DFS--IDA*算法

IDA*搭配迭代加深限制搜索层数depth

②估价函数用来判断该点到答案的理想步数(估价<=真实),如果已经走的步数+后续理想步数已经超过depth就不再往下搜索

③当前层数+估价函数>depth时可以剪枝


排书

180. 排书 - AcWing题库

题意:1~n乱序排放,每次操作可以移动连续的一段,求排好序的最少操作次数

思路: 

//判断有多少个后继是不正确的,每一步最多修改三个后继
int h(int w[]){
    int tot=0;
    for(int i=1;i<n;i++)
        if(w[i+1]!=w[i]+1)
            tot++;
    return (tot+2)/3;
}
bool check(int w[]){
	for(int i=1;i<=n;i++){
		if(w[i]!=i) return false;
	}
	return true;
}
//第u次操作 
bool dfs(int u,int w[],int depth){
	if(u+h(w)>depth) return false; 
	if(check(w)) return true;
	for(int len=1;len<=n;len++){
		for(int i=1;i+len-1<=n;i++){
			int j=i+len-1;
			//将[i,j]移到[k+1~] 
			for(int k=j+1;k<=n;k++){
				int op[N];
				int pos=1;
				for(int z=1;z<i;z++) op[pos++]=w[z];
				for(int z=j+1;z<=k;z++) op[pos++]=w[z];
				for(int z=i;z<=j;z++) op[pos++]=w[z];
				for(int z=k+1;z<=n;z++) op[pos++]=w[z];
				if(dfs(u+1,op,depth)) return true;
			}
		}
	}
	return false;
}
int main(){
	int t;cin>>t;
	while(t--){
		cin>>n;	
		for(int i=1;i<=n;i++){
			cin>>w[i];
		}
		int depth=0;
		while(!dfs(0,w,depth)){
			depth++;
			if(depth>=5) break;
		}
		if(depth>=5) cout<<"5 or more"<<endl;
		else cout<<depth<<endl;
	}
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vic.GoodLuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值