链接

把迭代加深理解错了 自己写了半天也没写对

所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索。这种方法虽然会导致重复的遍历 某些结点,但是由于搜索的复杂度是呈指数级别增加的,所以对于下一层搜索,前面的工作可以忽略不计,因而不会导致时间上的亏空。

IDA*就是一个加了层数限制depth的DFS,超过了限制就不在搜索下去,如果在当前层数没有搜到目标状态,就加大层数限制depth,这里还只是一个IDA算法,并不是A*的。当然我们可以用A*的估计函数去剪枝,如果当前深度d+h()>depth的时候就可以不再搜索下去了,这样就是IDA*了。

具体 代码里有注释

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int a[];
int p[] = {,,,,,,,};//中间8个所对应的序号
int rev[] = {,,,,,,,};//A-F B-E...反着移动
int v,ans[];
int po[][] = {,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,};//8种操作的原始顺序 对应ABCDEFGH
void change(int k)//操作一次的结果
{
int i,y = a[po[k][]];
for(i = ; i < ; i++)
a[po[k][i]] = a[po[k][i+]];
a[po[k][]] = y;
}
int fdep()//这个是简单的估计下还需要搜得层数 假如中间已经有5个相同的了 那最少还要移3次
{
int i,x[] = {,,,};
for(i = ; i < ; i++)
x[a[p[i]]]++;
int an=;
for(i = ; i < ; i++)
an = max(an,x[i]);
return -an;
}
int dfs(int depth)
{
int i,tt;
for(i = ; i < ; i++)
{
change(i);//操作
tt = fdep();
if(tt==)//已经到达目的解
{
ans[depth] = i;
return ;
}
if(depth+tt<v)//如果没有超过层数限制
{
ans[depth] = i;
if(dfs(depth+))
return ;
}
change(rev[i]);//撤销操作
}
return ;
}
int main()
{
int i;
while(scanf("%d",&a[])&&a[])
{
for(i = ; i < ; i++)
scanf("%d",&a[i]);
if(fdep()==)
{
puts("No moves needed");
printf("%d\n",a[]);//这里不要忘了输出
continue;
}
v = ;
while(!dfs())
{
v++;
}
for(i = ; i < v ; i++)
printf("%c",ans[i]+'A');
puts("");
printf("%d\n",a[]);
}
return ;
}

poj2286The Rotation Game(迭代加深dfs)的更多相关文章

  1. poj 3134 Power Calculus(迭代加深dfs+强剪枝)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  2. POJ 2248 - Addition Chains - [迭代加深DFS]

    题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...

  3. POJ-3134-Power Calculus(迭代加深DFS)

    Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...

  4. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  5. UVA1434-The Rotation Game(迭代加深搜索)

    Problem UVA1434-The Rotation Game Accept:2209  Submit:203 Time Limit: 3000 mSec  Problem Description ...

  6. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

  7. [poj 2331] Water pipe ID A*迭代加深搜索(dfs)

    Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...

  8. POJ1129Channel Allocation[迭代加深搜索 四色定理]

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14601   Accepted: 74 ...

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

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

随机推荐

  1. .net远程连接oracle数据库不用安装oracle客户端的方法

    .net远程连接oracle数据库不用安装oracle客户端的方法步骤: 1.添加Sytem.Data.OracleClient命名空间. 2.连接时需要ConnectionString字符串,出现在 ...

  2. 10_HTTP协议_入门知识

    [什么是HTTP协议] 对 浏览器客户端 和  服务器端之间的数据传输的格式规范. 客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵循一定的通讯格式,HTTP协议用于定义客户端与 ...

  3. ThinkPHP3.2 加载过程(四)

    前言: 由于比较懒散,但是又是有点强迫症,所以还是想继续把ThinkPHP3.2的加载过程这个烂尾楼补充完整. ========================================分割线= ...

  4. CentOS7 yum安装配置

    一.安装必要包 yum install gcc 二.linux下安装 #下载 wget http://download.redis.io/releases/redis-3.0.0.tar.gz tar ...

  5. 多线程Demo

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. 设置Session的超时时间

    设置Session的超时时间 IIS 里面有个设置 站点属性->主目录->应用程序配置->选项->启用会话状态->会话超时,可以设置. 在web.config中,设置: ...

  7. AVPlayer 视频播放

    1. AVPlayer AVPlayer 是一个用来播放基于时间的视听媒体的控制器对象(一个队播放和资源时间相隔信息进行管理的对象,而非一个视图或窗口控制器). AVPlayer支持播放从本地, 分步 ...

  8. div重叠不变形

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. hdu 5646 DZY Loves Partition 二分+数学分析+递推

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5646 题意:将n分成k个正整数之和,要求k个数全部相同:并且这k个数的乘积最大为多少?结果mod 1e^9 ...

  10. Vijos p1165 火烧赤壁 离散化+单调栈

    题目链接:https://vijos.org/p/1165 题意:输入n(n <= 20,000)段线段的端点,问所有线段的长度总和为多少? input: -1 1 5 11 2 9 outpu ...