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*

时间: 2024-07-30 23:45:47

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: 2153 Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2

(IDA*)POJ 2286 The Rotation Game

The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind. Initially, the blocks are placed on the board randomly. Your task is to

POJ - 2286 - The Rotation Game (IDA*)

IDA*算法,即迭代加深的A*算法,实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #include

【POJ 2286】 The Rotation Game

[题目链接] http://poj.org/problem?id=2286 [算法] IDA* [代码] #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #inclu

The Rotation Game (poj 2286 搜索IDA*)

Language: Default The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5573   Accepted: 1878 Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked

The Rotation Game (POJ 2286) 题解

[问题描述] (由于是英文的,看不懂,这里就把大意给大家说一下吧……都是中国人,相信大家也不愿意看英文……) 如图,一个井字形的棋盘,中间有着1-3任意的数,有ABCDEFGH八个操作,每个操作意味着该操作所在行朝该操作方向整体移动一格,详见图,你的目的是:对于输入的多组数据,用最少的步数使得井字形棋盘中间的八个数为同一个数,若不需操作就已达到要求,则输出“No moves needed”,无论是否需要操作,你都应将中间的数字给输出.输入以一个0结束. [样例输入] 1 1 1 1 3 2 3

搜索题推荐

(转自网络博客): POJ POJ 1376 – Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=1376 题意:略 解法:bfs,A*-. POJ 2688 – Cleaning Robot(基础) http://acm.pku.edu.cn/JudgeOnline/problem?id=2688 题意:bfs后转换为tsp问题 解法:bfs+dp,bfs+dfs 相关:http://hi.baidu.com/zfy0701/blo

POJ题目分类推荐 (很好很有层次感)

著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 3094) 初期: 一.基本算法: 枚举. (POJ 1753,POJ 2965) 贪心(POJ 1328,POJ 2109,POJ 2586) 递归和分治法. 递

POJ 刷题指南

OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 3094) 初期: 一.基本算法: 枚举. (POJ 1753,POJ 2965) 贪心(POJ 1328,POJ 2109,POJ 2586) 递归和分治法. 递推. 构造法.(POJ 3295) 模拟法.(POJ 1068,POJ 2632,POJ 1573,POJ 2993,POJ 2996) 二