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 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 move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting
of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A
and move C from some initial configuration.

Input

The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed
from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single `0‘ after the last
test case that ends the input.

Output

For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from `A‘ to `H‘, and there should not be any spaces between the letters in the line. If no moves are
needed, output `No moves needed‘ instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is
still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.

Sample Input

1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
0

Sample Output

AC
2
DDHH
2

Source

Shanghai 2004

题意:如图24个位置上有数字1~3,可以进行移动,每次对一条7个数进行平移,问怎样移动使得中心的8个方格为相同的数字,输出方案和最后中心的数字。

思路:IDA*,有八个操作,主要是这个移动操作不好弄,开一个辅助数组记录移动的位置关系。每移动一次中心改变一个数,以此构造h()。

代码:

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 30;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;

int Move[8][14]={
{1,3,3,7,7,12,12,16,16,21,21,23,23,1},
{2,4,4,9,9,13,13,18,18,22,22,24,24,2},
{11,10,10,9,9,8,8,7,7,6,6,5,5,11},
{20,19,19,18,18,17,17,16,16,15,15,14,14,20},
{24,22,22,18,18,13,13,9,9,4,4,2,2,24},
{23,21,21,16,16,12,12,7,7,3,3,1,1,23},
{14,15,15,16,16,17,17,18,18,19,19,20,20,14},
{5,6,6,7,7,8,8,9,9,10,10,11,11,5},
};

int ret[8]={5,4,7,6,1,0,3,2};//用于还原
int a[maxn],index,deep;
vector<char>ans;

void change(int x)
{
    int y=a[Move[x][0]];
    for (int i=0;i<12;i+=2)
        a[Move[x][i]]=a[Move[x][i+1]];
    a[Move[x][12]]=y;
}

int h(int &x)
{
    int num[4]={0};
    for (int i=7;i<=9;i++) num[a[i]]++;
    for (int i=16;i<=18;i++) num[a[i]]++;
    num[a[12]]++; num[a[13]]++;
    int Max=0;
    for (int i=1;i<=3;i++)
    {
        if (Max<num[i])
        {
            Max=num[i];
            x=i;
        }
    }
    return 8-Max;
}

bool dfs(int k)
{
    if (k>deep) return false;
    int x,y;
    x=h(y);
    if (k+x>deep) return false;
    if (x==0)
    {
        index=y;
        return true;
    }
    for (int i=0;i<8;i++)
    {
        change(i);
        ans.push_back(i+'A');
        if (dfs(k+1)) return true;
        ans.pop_back();
        change(ret[i]);
    }
    return false;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
    int i,j;
    while (scanf("%d",&a[1]))
    {
        if (a[1]==0) break;
        for (i=2;i<=24;i++) scanf("%d",&a[i]);
        int x,y;
        x=h(y);
        if (x==0){
            printf("No moves needed\n");
            printf("%d\n",y);
            continue;
        }
        deep=1;
        ans.clear();
        while (1)
        {
            if (dfs(0)) break;
            deep++;
        }
        for (int i=0;i<ans.size();i++)
            printf("%c",ans[i]);
        printf("\n%d\n",index);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 18:27:46

The Rotation Game (poj 2286 搜索IDA*)的相关文章

POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*

感觉这种算法还是比较局限的吧,重复搜索是一个不好的地方,而且需要高效的估值函数来进行强剪枝,这点比较困难. 迭代搜索深度是一个比较炫酷的搜索方式,不过有点拿时间换空间的感觉. 首先迭代深度比较搓的写法是,首先设置一个阀值MaxH,初始为最小值. 当在搜索深度Depth <= MaxH时找到解则此时为最优解,否则MaxH++,继续深搜. 另外一种比较吊的写法是二分搜索深度,若搜到则减小阀值,否则增大阀值. 总之,迭代深度搜索就是通过改变深搜的深度来寻找最优解,这样做的好处是省掉了BFS中状态标记所

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

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

迭代加深搜索 自己看的时候第一遍更本就看不懂..是很水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题并且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的见解, 真的是很有帮助,也许自己想要想很久才能想明白,还会很痛苦,稍微问一下别人的想法,点上一个方向,剩下的自己就能想得明白了. 迭代加深. 把answer(需要的步数或其他)在主函数里面从零往上递加,此之谓 "层数",亦可谓之"深度".用书上的话就是: 从小到大枚举深度

poj 2531 搜索剪枝

Network Saboteur Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description A university network is composed of N computers. System administrators gathered informat

poj 1129 搜索

Channel Allocation Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description When a radio station is broadcasting over a very large area, repeaters are used to ret

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

(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

UVa 1343 The Rotation Game (状态空间搜索 &amp;&amp; IDA*)

题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使中心8个格子数字一致的最少步骤,要输出具体的操作步骤及最终中心区域的数字.如果有多个解,输出字典序最小的操作步骤. 分析 : 还是状态空间的搜索,对象就是一个数字序列,判断中心位置是否一样,可以看出如果使用BFS,每一层还是爆炸,所以使用IDA*,关键还是模拟操作和h函数,这里的h函数是这样定义的,