本章聚集了一些做了的迭代启发式搜索的题目

为什么只打了迭代启发式搜索?

因为它很好打,有些类似迭代的时候加的最优化剪枝

[因为这个最优化剪枝其实就是你算的估价函数了...]

BZOJ 1085 骑士精神

比较经典的一题,特别是题目中还说了在15步内走完...看上去就是迭代的样子

估价函数->指棋盘上不符合规定的骑士数目...每次移动最多让一名骑士回到自己的位置,由此来剪枝

 #include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; int maxd,sx,sy;
char ch[][];
int x[]={,,-,-,,,-,-};
int y[]={,-,,-,,-,,-};
int ans[][]={{,,,,},{,,,,},{,,,,},{,,,,},{,,,,}}; struct Node{
int a[][];
int illegal(){
int cnt=;
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a[i][j]!=ans[i][j]) cnt++;
return cnt;
}
}; inline bool judge(int a[][]){
for(int i=;i<;i++)
for(int j=;j<;j++)
if(a[i][j]!=ans[i][j]) return false;
return true;
} inline bool inmap(int x,int y){
if(x>= || x< || y>= || y<) return false;
return true;
} bool dfs(int level,Node s,int bx,int by){
if(!level){
if(judge(s.a)) return true;
return false;
}
int newx,newy;
for(int i=;i<;i++){
newx=bx+x[i],newy=by+y[i];
if(inmap(newx,newy)){
swap(s.a[bx][by],s.a[newx][newy]);
if(s.illegal()<=level)
if(dfs(level-,s,newx,newy)) return true;
swap(s.a[bx][by],s.a[newx][newy]);
}
}
return false;
} int main(){
#ifndef ONLINE_JUDGE
freopen("1085.in","r",stdin);
freopen("1085.out","w",stdout);
#endif
int kase;
Node s;
scanf("%d",&kase);
while(kase--){
for(int i=;i<;i++){
scanf("%s",ch[i]);
for(int j=;j<;j++)
if(ch[i][j]=='*') s.a[i][j]=,sx=i,sy=j;
else if(ch[i][j]=='') s.a[i][j]=;
else s.a[i][j]=;
}
if(!s.illegal()) {puts("");continue;}
for(maxd=;maxd<=;maxd++)
if(dfs(maxd,s,sx,sy)) break;
if(maxd<=)
printf("%d\n",maxd);
else
puts("-1");
}
return ;
}

codevs1288 埃及分数

强行把从某个状态之后的最小值看做估价函数也是可以的...紫书例题

不过有一些坑点,看codevs题解的第一篇吧(...我写的)。

 #include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; typedef long long ll; const int maxn=;
const int INF=0x7f7f7f7f; int a,b,d;
ll res[maxn],rec[maxn]; ll gcd(ll a,ll b){
ll t;
while(b)
t=b,b=a%b,a=t;
return a;
} inline bool better(ll a[]){
if(a[]>=res[]) return false;
else return true;
for(int i=;i<=d;i++)
if(a[i]<res[i]) return true;
else if(a[i]>res[i]) return false;
return false;
} bool dfs(int level,ll last,ll aa,ll bb){
if(level==){
if(bb%aa) return false;
rec[]=bb/aa;
if(better(rec))
for(int i=;i<=d;i++) res[i]=rec[i];
return true;
}
bool ok=false;
last=max(last+,(bb/aa)+!(bb%aa==));
for(int i=last;;i++){
if(aa*i>=bb*level) break;
ll b1=bb*i,a1=aa*i-bb,Gcd;
Gcd=gcd(a1,b1);
a1/=Gcd,b1/=Gcd;rec[level]=i;
if(dfs(level-,i,a1,b1)) ok=true;
}
return ok;
} int main(){
scanf("%d%d",&a,&b);
memset(res,0x7f,sizeof(res));
for(d=;!dfs(d,,a,b);d++);
for(int i=d;i>=;i--)
printf("%lld ",res[i]); return ;
}

codevs2495 水叮当的舞步

这题估价函数居然就是一个颜色?...我其实有想过什么连通块个数的估价然而没有什么用...

其实这题关键还是在于给左上角染色的过程,每次给变色的地方染1,变色的周围染2,然后每次的颜色只要扩展2中相同的就好...比每次dfs快上很多的样子...

 #include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; int maxd,n;
int mp[][],mark[][];
int xx[]={,-,,};
int yy[]={,,-,};
bool used[];
bool ans; int get(){
int cnt=;
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(!used[mp[i][j]] && mark[i][j]!=)
used[mp[i][j]]=,cnt++;
return cnt;
} void dfs(int a,int b,int x){
mark[a][b]=;
for(int i=;i<;i++){
int nowx=a+xx[i],nowy=b+yy[i];
if(nowx<||nowy<||nowx>n||nowy>n||mark[nowx][nowy]==) continue;
mark[nowx][nowy]=;
if(mp[nowx][nowy]==x)dfs(nowx,nowy,x);
}
} int fill(int x){
int cnt=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(mark[i][j]==&&mp[i][j]==x)
cnt++,dfs(i,j,x);
return cnt;
} bool search(int level){
int v=get();
if(!v) return true;
if(v>level) return false;
int temp[][];
for(int i=;i<=;i++){
memcpy(temp,mark,sizeof(mark));
if(fill(i)) if(search(level-)) return true;
memcpy(mark,temp,sizeof(mark));
}
return false;
} int main(){ while(~scanf("%d",&n)){
if(!n) break;
memset(mark,,sizeof(mark));
ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&mp[i][j]);
dfs(,,mp[][]);
for(maxd=;!search(maxd);maxd++);
printf("%d\n",maxd);
}
return ;
}

UVa11212 编辑书稿

估价函数比较精妙,每次最多让三个元素的后继正确。

为什么是后继呢?因为这个操作的本质就是改变后继...

 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; const int maxn=; struct Node{
int v[maxn];
}; int n,maxd; inline int get_h(int a[]){
int cnt=;
for(int i=;i<n;i++)
if(a[i+]!=a[i]+) cnt++;
return cnt;
} void move(Node &Start,Node &Next,int I,int J,int K){
for(int i=;i<=K-J-;i++)
Next.v[i+I]=Start.v[J++i];
for(int i=;i<=J-I;i++)
Next.v[K-J+I+i]=Start.v[i+I];
} bool dfs(int level,Node s){
if(get_h(s.v)>*level) return false;
if(get_h(s.v)==) return true; Node next;
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++)
for(int k=j+;k<=n;k++){
memcpy(next.v,s.v,sizeof(s.v));
move(s,next,i,j,k);
if(dfs(level-,next)) return true;
}
return false;
} int main(){
#ifndef ONLINE_JUDGE
freopen("11212.in","r",stdin);
freopen("11212.out","w",stdout);
#endif int kase=;
Node a0; while(~scanf("%d",&n)){
if(!n) break;
for(int i=;i<=n;i++)
scanf("%d",&a0.v[i]);
if(!get_h(a0.v)){
printf("Case %d: 0\n",++kase);
continue;
} for(maxd=;!dfs(maxd,a0);maxd++);
printf("Case %d: %d\n",++kase,maxd);
} return ;
}

迭代启发式搜索 IDA*的更多相关文章

  1. 【2020-8-21】【数字游戏】【启发式搜索IDA*】

    有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一个例子: ...

  2. 【题解】AcWing 193. 算乘方的牛

    原题链接 题目描述 约翰的奶牛希望能够非常快速地计算一个数字的整数幂P(1 <= P <= 20,000)是多少,这需要你的帮助. 在它们计算得到最终结果的过程中只能保留两个工作变量用于中 ...

  3. 【Uva 12558】 Egyptian Fractions (HARD version) (迭代加深搜,IDA*)

    IDA* 就是iterative deepening(迭代深搜)+A*(启发式搜索) 启发式搜索就是设计估价函数进行的搜索(可以减很多枝哦~) 这题... 理论上可以回溯,但是解答树非常恐怖,深度没有 ...

  4. 小结:A* & IDA* & 迭代深搜

    概要: 在dfs中,如果答案的深度很小但是却很宽,而且bfs还不一定好做的情况下,我们就综合bfs的优点,结合dfs的思想,进行有限制的dfs.在这里A*.IDA*和迭代深搜都是对dfs的优化,因此放 ...

  5. 迭代,IDA*

    1.codevs1288 题意:对于一个分数a/b(a!=1),将它表示为1/x + 1/y + 1/z ……的形式,x,y,z……互不相同 多解取加数少的,加数相同时,取最小的分数最大的. 思路:经 ...

  6. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  7. 迭代加深 A* IDA* 初探

    并没有有用的东西, 只是用来水的. 今天看搜索,想起来了A*和IDA* 看A*去了.... 啥玩意啊这是,他们代码为啥这么长??.... 看完了,...代码怎么写啊?? .....算了,直接看题吧 找 ...

  8. UVA 11212 Editing a Book [迭代加深搜索IDA*]

    11212 Editing a Book You have n equal-length paragraphs numbered 1 to n. Now you want to arrange the ...

  9. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

随机推荐

  1. partial class 说明

    C# 2.0 可以将类.结构或接口的定义拆分到两个或多个源文件中,在类声明前添加partial关键字即可. 例如:下面的PartialTest类 class PartialTest { string ...

  2. SDUST 软件工程2016-作业4-A 百钱买鸡问题

    解决百钱买鸡问题原本并不困难,关键的是这道题对其进行了升级,测试数据太大,传统的解法,像三重循环,二重循环都会导致超时. 这道题正确的解法应该是结合数学方程进行化简,将其转化为1层循环: x+y+z= ...

  3. How to move the user document folder to D disk[Windows 7]

    when you install windows 7 OS, the system ask for you enter username and password, then you have not ...

  4. C# 平时碰见的问题【5】

    vs按F5启动调试,项目不会编译的解决办法 工具 -> 选项 -> 项目和解决方案 -> 运行时, 当项目过期(下拉框) -> 不要选[从不生成] 附英文版的:

  5. python学习第二天第一部分

    备注:写程序不能写重复性的代码 学习内容:数据类型.for循环.while循环.字符编码.文件处理 一.for循环 1.简单的for循环 for i in range(10): # 此处意思为:循环r ...

  6. web开发中,前端javascript代码的组织结构

    网页包含三个层次: 结构(HTML) 表现(CSS) 行为(javascript) web标准中,三者要分离,网页源代码由三部分组成:.html文件..css文件和.js文件.就是说html文件中不应 ...

  7. wpf做的可扩展记事本

    记得有个winform利用反射做的可扩展笔记本,闲来无事,便用wpf也搞了个可扩展记事本,可用接口动态扩展功能,较简单,以便参考: 目录结构如下: MainWindow.xaml为主功能界面,Func ...

  8. next permutaion算法

    算法描述: Find largest index i such that array[i − 1] < array[i]. Find largest index j such that j ≥ ...

  9. 1092. To Buy or Not to Buy (20)

    Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy ...

  10. Cron表达式说明

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表. CronT ...