博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVa 11212 - Editing a Book <状态空间搜索+IDA*算法>
阅读量:4569 次
发布时间:2019-06-08

本文共 1357 字,大约阅读时间需要 4 分钟。

本题的关键是应用IDA*算法,降低时间复杂度,启发函数的发现和推导是重点。

对于可以用回溯法求解但解答树的深度没有明显上限的题目,可以考虑使用迭代加深搜索。设计一个乐观估计函数,预测从当前节点至少还学要扩展几层才有可能得到解,则状态空间搜索变成了IDA*算法。

刚开始吧状态定义为typedef  int  State[12],但后来发现编译器老是报错并且极易出现各种错误,索性直接定义为一个struct。

#include 
using namespace std;struct State{ int a[12];};int n, kase = 0, maxd;int getH(State cur){ int cnt = 0; for(int i = 0; i < n-1; ++i) if(cur.a[i]+1 != cur.a[i+1]) cnt++; return cnt;}bool DFS(int d, State u){ int H = getH(u); if(d == maxd) return H == 0; if(3*d + H > 3*maxd) return false; for(int x = 0; x < n - 1; ++x) for(int l = 1; l <= x+1; ++l) for(int r = 1; r <= n-x-1; ++r){ State v; memcpy(v.a, u.a, sizeof(u.a)); for(int i = x - l + 1, j = x + r - l + 1; i <= x; ++i, ++j) v.a[j] = u.a[i]; for(int i = x + 1, j = x - l + 1; i <= x + r; ++i, ++j) v.a[j] = u.a[i]; if(DFS(d + 1, v)) return true; } return false;}int main(){ ios::sync_with_stdio(false); while(cin >> n && n){ State beg; for(int i = 0; i < n; ++i) cin >> beg.a[i]; for(maxd = 0; ; ++maxd) if(DFS(0, beg)) break; printf("Case %d: %d\n", ++kase, maxd); } return 0;}

转载于:https://www.cnblogs.com/kunsoft/p/5312725.html

你可能感兴趣的文章
Java开源工具 网站开发工具清单
查看>>
POJ 1442 Black Box
查看>>
Python 内置模块:os模块
查看>>
C# 中的特性 Attribute
查看>>
Microsoft SQL Server, Error: 15128 ()
查看>>
学《数据结构》有感
查看>>
eclipse下如何关联android-support-v4.jar源码
查看>>
§ 理论基础
查看>>
iis实现点击文件下载而不是打开文件
查看>>
Atitit. . 软件命名空间与类名命名单词的统计程序设计v2
查看>>
Atitit.如何建立研发体系
查看>>
HttpHandler给本站加图片水印
查看>>
HTML Music Entities/音乐符号
查看>>
Linux signal 处理
查看>>
Oracle中merge into语法
查看>>
Vue2.x + vux2.x + vux-loader + typescript 搭建第一个环境
查看>>
MySQL的binlog日志
查看>>
vagrant The specified host network collides with a non-hostonly network!
查看>>
0x59 单调队列优化DP
查看>>
mysql中的union用法
查看>>