UVA - 1343 The Rotation Game : IDA*

题目点此跳转

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

思路

 题目意思是:如图所示形状的棋盘上分别有8个1、2、3,要往A~H方向旋转棋盘,使中间8个方格数字相同。图(a)进行A操作后变为图(b),再进行C操作后变为图(c),这正是一个目标状态(因为中间8个方格数字相同)。要求旋转次数最少。如果有多解,操作序列的字典序应尽量小。

 典型的状态空间搜索, 这里使用IDA*, h值使用中间8个方格出现最多的数字的出现次数.
 这样每次操作最多会使h值增加1,所以当搜索深度maxd-当前深度deep(即还能走几步) < 8-h时,则说明在当前maxd下是肯定找不到答案的,可以剪掉.这是对搜索下界的一个剪枝.
 还有一点,转移状态时要排除和上次操作相反的操作,否则会造成死循环.

代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a,b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
#define OT freopen("out.txt", "w", stdout);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e2 + 10;
const int INF = 0x7fffffff;
const int dir[5][2] = {0,0,-1,0,1,0,0,-1,0,1};

int a[65], maxd, ans, res[100007];
int ops[10] = { 5,4,7,6, 1,0,3,2 };

bool read() {
    //met(a, 0);
    scanf("%d", &a[1*7+3]);
    if(a[1*7+3] == 0) return 0;
    scanf("%d%d%d", &a[1*7+5], &a[2*7+3], &a[2*7+5]);
    for(int i = 1; i <= 7; ++i) scanf("%d", &a[3*7+i]);
    scanf("%d%d", &a[4*7+3], &a[4*7+5]);
    for(int i = 1; i <= 7; ++i) scanf("%d", &a[5*7+i]);
    scanf("%d%d%d%d", &a[6*7+3], &a[6*7+5], &a[7*7+3], &a[7*7+5]);
    return 1;
}

void ot() {
    for(int i = 1; i <= 7; ++i){
        for(int j = 1; j <= 7; ++j) printf("%d", a[i*7+j]);
        printf("\n");
    }
}

int check() {
    int ans = a[3*7+3];
    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 3; ++j) {
            if(i == 1 && j == 1) continue;
            if(a[(3+i)*7+3+j] != ans) return 0;
        }
    }
    return ans;
}

void moves(int i) {
    int tmp;
    switch(i) {
        case 0:
            tmp = a[1*7+3];
            for(int i = 1; i <= 6; ++i) a[i*7+3] = a[(i+1)*7+3];
            a[7*7+3] = tmp; break;
        case 1:
            tmp = a[1*7+5];
            for(int i = 1; i <= 6; ++i) a[i*7+5] = a[(i+1)*7+5];
            a[7*7+5] = tmp; break;
        case 2:
            tmp = a[3*7+7];
            for(int i = 7; i >= 2; --i) a[3*7+i] = a[3*7+i-1];
            a[3*7+1] = tmp; break;
        case 3:
            tmp = a[5*7+7];
            for(int i = 7; i >= 2; --i) a[5*7+i] = a[5*7+i-1];
            a[5*7+1] = tmp; break;
        case 4:
            tmp = a[7*7+5];
            for(int i = 7; i >= 2; --i) a[i*7+5] = a[(i-1)*7+5];
            a[1*7+5] = tmp; break;
        case 5:
            tmp = a[7*7+3];
            for(int i = 7; i >= 2; --i) a[i*7+3] = a[(i-1)*7+3];
            a[1*7+3] = tmp; break;
        case 6:
            tmp = a[5*7+1];
            for(int i = 1; i <= 6; ++i) a[5*7+i] = a[5*7+i+1];
            a[5*7+7] = tmp; break;
        case 7:
            tmp = a[3*7+1];
            for(int i = 1; i <= 6; ++i) a[3*7+i] = a[3*7+i+1];
            a[3*7+7] = tmp; break;
    }
}

int getH() {
    int cnt[4] = {0};
    for(int i = 0; i < 3; ++i) cnt[a[3*7+3+i]]++, cnt[a[5*7+3+i]]++;
    cnt[a[4*7+3]]++; cnt[a[4*7+5]]++;
    return max(max(cnt[1], cnt[2]), cnt[3]);
}
bool dfs(int p, int deep) {
    int h = getH();
    if(maxd-deep < 8 - h) return 0;
    if(deep == maxd) {
        ans = check();
        if(ans > 0) return 1;
        return 0;
    }
    int tmp[65];
    memcpy(tmp, a, sizeof(tmp));
    for(int i = 0; i < 8; ++i) {
        if(ops[i] == p) continue;
        moves(i); res[deep] = i;
        if(dfs(i, deep+1)) return 1;
        memcpy(a, tmp, sizeof(a));
    }
    return 0;
}

void IDAS() {
    ans = check();
    if(ans > 0) { printf("No moves needed\n%d\n", ans); return; }
    for(maxd = 1; ; ++maxd) { if(dfs(-1, 0)) break; }
    for(int i = 0; i < maxd; ++i) printf("%c", res[i]+'A'); printf("\n");
    printf("%d\n", ans);
}

int main() {
    #ifdef _LOCAL
    IN; //OT;
    #endif // _LOCAL

    while(read()) { IDAS(); }


    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值