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 ...
随机推荐
- [译]JavaScript insertAdjacentHTML
原文地址:http://davidwalsh.name/insertadjacenthtml-beforeend 该死的DOM慢的很.随着我们的网站动态交互和Ajax操作越来越多,我们需要寻找一种高性 ...
- java集合_collection 中的方法 通过Arraylist来体现
import java.util.*; /* Collection定义了集合框架的共性功能.1,添加 add(e); addAll(collection); 2,删除 remove( ...
- dorado中的creationType选择类型
新建model层中 DataType类型的时候,有几个属性creationType,matchType时候需要在右侧选择对应的javaBean,这是时候要在弹出的对话框搜索. 此时,只要搜索javaB ...
- WPF FileFolderDialog 和弹出子窗口的一些问题
摘要:本文主要是WPF中 FileFolderDialog的相关问题,补充了关于在父窗口弹出子窗口,以及子窗口的相关属性(Data Binding)和命令绑定(Delegate Command)问题, ...
- ssh连接失败解决方法
执行如下命令: ssh-keygen -t dsa -P '' -f /etc/ssh/ssh_host_dsa_key ssh-keygen -t rsa -P '' -f /etc/ssh/ssh ...
- mysql 之权限介绍
转自:http://tech.it168.com/a2010/0114/837/000000837456_all.shtml 一.MySQL授权表概述首先从全局开始,如果全局的是允许的,即在 user ...
- Jquery Offset, Document, Window 都是什么
From http://www.cnblogs.com/luhe/archive/2012/11/14/2769263.html JQuery Offset实验与应用 我们有时候需要实现这样一种功 ...
- sae-xhprof调试性能
1. 在storage中创建xhprof的domain 2.在xhprof中,给对应的版本应用开启调试 3.在版本内的代码加入 sae_xhprof_start(); // 需要调优的代码 // .. ...
- 51nod1079中国剩余定理
/** *中国剩余定理 */ #include<iostream> #include<cstdio> #include<map> #include<cstri ...
- SQL技术内幕四
数据类型: sql server只接受两种数据类型 1. 普通字符 varchar char 用一个字节表示一个字符,表示英文 2.unicode nchar nvarchar 用两个字节表示一个 ...