The Rotation Game (poj 2286 搜索IDA*)

46 篇文章 0 订阅

Language:
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




题意:如图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;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值