poj2286The Rotation Game(迭代加深dfs)
把迭代加深理解错了 自己写了半天也没写对
所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索。这种方法虽然会导致重复的遍历 某些结点,但是由于搜索的复杂度是呈指数级别增加的,所以对于下一层搜索,前面的工作可以忽略不计,因而不会导致时间上的亏空。
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)的更多相关文章
- poj 3134 Power Calculus(迭代加深dfs+强剪枝)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
- POJ 2248 - Addition Chains - [迭代加深DFS]
题目链接:http://bailian.openjudge.cn/practice/2248 题解: 迭代加深DFS. DFS思路:从目前 $x[1 \sim p]$ 中选取两个,作为一个新的值尝试放 ...
- POJ-3134-Power Calculus(迭代加深DFS)
Description Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multipli ...
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
- UVA1434-The Rotation Game(迭代加深搜索)
Problem UVA1434-The Rotation Game Accept:2209 Submit:203 Time Limit: 3000 mSec Problem Description ...
- Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)
An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...
- [poj 2331] Water pipe ID A*迭代加深搜索(dfs)
Water pipe Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 2265 Accepted: 602 Description ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
随机推荐
- IOS开发之NSObject协议类方法说明
oc中NSObject类是所有类的基类,所有类都要继承自它,那么它的方法就显得特别重要,因为所有类都会有这些基本的方法. 看看oc的源码中NSObject是这样定义的: @interface NSOb ...
- ECMAScript 6十大特性
ES6入门 http://es6.ruanyifeng.com/ ES6排名前十的最佳特性列表 Default Parameters(默认参数) in ES6 Template Literals (模 ...
- STM32F407移植contiki2.6后使用LWIP库实现tcp client
最近在做智能家居,物联网项目,用到了C下的contiki移植 经过一阵调试,终于在 STM32F407移植contiki2.6后使用LWIP库实现tcp client. 一路艰辛谁人知道....唯有留 ...
- SAP Java Connector(JCo)
JCo是一个高性能的,基于JNI的中间件,它实现了SAP的RFC(Remote Function Call)协议. 1.JCo的安装 从 http://files.cnblogs.com/byfhd/ ...
- Delphi新语法和ifthen的扩展联想
Delphi之前已经重载了好多个ifthen函数 Math单元 ): Integer; overload; inline; ): Int64; overload; inline; ): UInt64; ...
- Socket 入门
int socket(int domain, int type,int protocol) domain :说明我们网络程序所在的主机采用的通讯协族(AF_UNIX和AF_INET等). AF_UNI ...
- 七,WPF的元素绑定
数据绑定是一种关系,该关系告诉WPF从一个源对象提取一些信息,并使用这些信息设置目标对象的属性,目标属性总是依赖项属性,然而,源对象可以是任何内容. 源对象是WPF元素并且源属性是依赖项属性的数据绑定 ...
- 如何根据w3wp.exe的进程pid查看是哪个应用程序池
如何根据w3wp.exe的进程pid查看是哪个应用程序池? 根据iisapp 查看PID所对应的IIS应用程序池及详细介绍: 从IIS6.0可以在IIS中架设多个站点并给每个站点指定不同的应用程序池, ...
- c#让窗体永在最前 调用windows api 将窗体设为topmost
有时候应用程序需要将一个窗体始终位于屏幕的最前面,即使切换到其它窗体也能看到该窗体,这样的窗体就叫做TopMost窗体. 用C#制作TopMost窗体之前,首先要了解如何声明SetWindowPos函 ...
- IOS中利用宏将RGB值转换为UIColor(转)
可以在pch文件中定义宏,这样整个项目就都可以用了! #define UIColorFromRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgb ...