POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*
感觉这样的算法还是比較局限的吧,反复搜索是一个不好的地方,并且须要高效的估值函数来进行强剪枝,这点比較困难。
迭代搜索深度是一个比較炫酷的搜索方式,只是有点拿时间换空间的感觉。
首先迭代深度比較搓的写法是,首先设置一个阀值MaxH,初始为最小值。
当在搜索深度Depth <= MaxH时找到解则此时为最优解,否则MaxH++,继续深搜。
第二种比較吊的写法是二分搜索深度,若搜到则减小阀值,否则增大阀值。
总之,迭代深度搜索就是通过改变深搜的深度来寻找最优解,这样做的优点是省掉了BFS中状态标记全部的空间花销。
对于这样的算法掌握的并不透彻,就不多说了。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 6000007 using namespace std; int num[25]; int order[8][7] ={
{ 1, 3, 7,12,16,21,23},
{ 2, 4, 9,13,18,22,24},
{11,10, 9, 8, 7, 6, 5},
{20,19,18,17,16,15,14},
{24,22,18,13, 9, 4, 2},
{23,21,16,12, 7, 3, 1},
{14,15,16,17,18,19,20},
{ 5, 6, 7, 8, 9,10,11}
}; int MaxH; int Cal(int *num)
{
int mark[] = {0,0,0,0,0}; for(int i = 7;i <= 9; ++i)
mark[num[i]]++;
for(int i = 12;i <= 13; ++i)
mark[num[i]]++;
for(int i = 16;i <= 18; ++i)
mark[num[i]]++;
return min(8-mark[1],min(8-mark[2],8-mark[3]));
} int sta[1000];
int Top;
int anw; bool dfs(int *num,int ans)
{
int temp = Cal(num); if(temp == 0)
{
anw = num[8];
return true;
} if(temp + ans > MaxH)
return false; int tn[25]; int i,j; for(i = 0;i < 8; ++i)
{
sta[Top++] = i;
for(j = 1;j <= 24; ++j)
tn[j] = num[j];
for(j = 0;j < 7; ++j)
tn[order[i][j]] = num[order[i][(j+1)%7]];
if(dfs(tn,ans+1))
return true;
Top--;
}
return false;
} int main()
{
int i; while(scanf("%d",&num[1]) && num[1])
{
for(i = 2;i <= 24; ++i)
scanf("%d",&num[i]); if(Cal(num) == 0)
{
printf("No moves needed\n");
printf("%d\n",num[7]);
continue;
} MaxH = 1;
Top = 0; while(dfs(num,0) == false)
{
MaxH++;
}
for(i = 0;i < Top; ++i)
printf("%c",sta[i]+'A');
printf("\n%d\n",anw);
} return 0;
}
POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*的更多相关文章
- POJ 2286 The Rotation Game(IDA*)
The Rotation Game Time Limit: 15000MS Memory Limit: 150000K Total Submissions: 6396 Accepted: 21 ...
- POJ - 2286 - The Rotation Game (IDA*)
IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...
- [poj] 2286 The Rotation Game || ID-DFS
原题 有1234四个数字,每个数字八个.有八种方向的移动,使得操作后中间八个方块的数字相同,求最小操作步数. 对于这种求最小步数的看起来就是dfs的题,就ID-DFS就好了. //不知道为什么都是ID ...
- POJ 2286 The Rotation Game IDA*
(再一次感谢学长幻灯片) ID A* 随便自己yy了一下. 额嗯 思路什么的都没有问题 就是改不对.. 无奈地删代码...边删边交. 删啊删 哎呦 AC了 ... ... ... 找删的那一段 . o ...
- 【POJ 2286】 The Rotation Game
[题目链接] http://poj.org/problem?id=2286 [算法] IDA* [代码] #include <algorithm> #include <bitset& ...
- The Rotation Game (POJ 2286) 题解
[问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着 ...
- ACM北大暑期课培训第四天
今天讲了几个高级搜索算法:A* ,迭代加深,Alpha-Beta剪枝 以及线段树 A*算法 启发式搜索算法(A算法) : 在BFS算法中,若对每个状态n都设定估价函数 f(n)=g(n)+h(n) ...
- poj很好很有层次感(转)
OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...
- POJ题目分类推荐 (很好很有层次感)
著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0203格式化输出
题目 解决代码及点评 #include <stdio.h> #include <stdlib.h> void main() { // print是输出函数,参数%s表示输 ...
- prim模板
]; int n; ][]; ]; int prim(){ int i,j,mi,v; ;i<n;i++){ d[i]=map[][i]; vis[i]=; } ;i<=n;i++){ m ...
- if语句求三个数中最大的
Console.WriteLine("请输入第一个数:"); int a = Convert.ToInt32( Console.ReadLine()); Console.Write ...
- 【linux】 linux gpio操作
欢迎转载,转载时需保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...
- [Swust OJ 137]--波浪数(hash+波浪数构造)
题目链接:http://acm.swust.edu.cn/problem/137/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- CentOS6.5 下在Nginx中添加SSL证书以支持HTTPS协议访问
参考文献: 1. NginxV1.8.0安装与配置 2. CentOS下在Nginx中添加SSL证书以支持HTTPS协议访问 3. nginx配置ssl证书的方法 4.nginx强制使用https访问 ...
- java.util.zip.Deflater 压缩 inflater解压 实例
原文:java压缩解压缩类实例[转] package com.example.helloworld; import java.io.ByteArrayOutputStream; import java ...
- jquery 单击li防止重复加载的实现代码
因为加载内容比较慢,所以用户可能在li上不经意点击了两次,那么就会请求两次,这是我们不想看到的. 今天在javascript-jquery群上一筒子发了两个demo给我,他的方法是先将单击的li节点拷 ...
- yii_wiki_204_using-cjuidialog-to-edit-rows-in-a-cgridview(通过CJuiDialog在CGridView中修改行数据)
/*** Using CJuiDialog to edit rows in a CGridView http://www.yiiframework.com/wiki/204/using-cjuidia ...
- 基于visual Studio2013解决C语言竞赛题之0423比赛安排
题目