# 搜索专题4 | 旋转棋盘 POJ - 2286

## 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. 这时一个井字游戏，如下图，有24个方格。每个格子用1/2/3进行标记，且每个数字只会出现8次。

image

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. 刚开始地时候，每一小块随机地放在棋盘上。现在需要你通过移动block，使得中间地8个格子地数字相同。只有一种有效操作，那就是旋转4条线中的一条，每条线有7个格子。旋转操作其实是将6个格子往对应方向移动一格，而最前面的格子会移动到末尾。这样有8种移动方式，每种方式标记为A-H。上图中演示了如何移动A/移动C的操作结果。

## 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. 输入不超过30个用例。每个用例包含24个数字，数字从上往下从左往右依次记录。

## 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. 输出分为2行，第一行为操作序列，第二行输出中间的数字。

## 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

IDA*算法和A*算法都是启发式搜索，都会用到估值函数。上一篇中我们了解到A*算法的估值函数主要用于优先队列的比较中。而IDA*的估值函数用在哪呢？

IDA*算法则是DFS+迭代加深搜索+估值函数，迭代加深比较容易理解，也就是在搜索过程中记录搜索的深度，如果超过深度就不继续搜索。注意DFS算法基本上都是需要进行回溯还原现场处理的。比如本题中的旋转操作，每一次旋转，在搜索完成时都会还原这个旋转操作。估值函数在DFS中往往用于剪枝操作，本题中将剩余数字作为估值函数，判断是否需要继续搜索。

```#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

// 8个方向产生的影响
int rot[8][7] = {
0,  2,  6,  11, 15, 20, 22,  // A
1,  3,  8,  12, 17, 21, 23,  // B
10, 9,  8,  7,  6,  5,  4,   // C
19, 18, 17, 16, 15, 14, 13,  // D
23, 21, 17, 12, 8,  3,  1,   // E
22, 20, 15, 11, 6,  2,  0,   // F
13, 14, 15, 16, 17, 18, 19,  // G
4,  5,  6,  7,  8,  9,  10   // H
};

// 反方向
int res[] = {5, 4, 7, 6, 1, 0, 3, 2};

int depth;

bool check(char s[]) {
// s[6]是中间的位置
char ch = s[6];
// 比较上下
for (int i = 0; i < 3; i++)
if (ch != s[i + 6] || ch != s[15 + i])
return false;
//比较腰部2个
return ch == s[11] && ch == s[12];
}

void rotate(int k, char s[]) {
char ch = s[rot[k][0]];
// 移动6个位置
for (int i = 0; i < 6; i++)
s[rot[k][i]] = s[rot[k][i + 1]];
// 移动开头位置
s[rot[k][6]] = ch;
}

// k表示当前深度
// st是棋盘
// op是当前操作
// la是上一次的操作
bool IDAstar(int k, char st[], char op[], int la) {
int cnt[4];
cnt[1] = cnt[2] = cnt[3] = 0;

for (int i = 0; i < 3; i++)
cnt[st[i + 6] - '0']++, cnt[st[15 + i] - '0']++;

cnt[st[11] - '0']++, cnt[st[12] - '0']++;
// cnt[0]是最大值，也就是1/2/3中数字最多的数量
cnt[0] = max(max(cnt[1], cnt[2]), cnt[3]);

// 大于深度就不再搜索了，注意这里8-cnt[0]就是估值函数h
if (k + 8 - cnt[0] >= depth)
return false;

for (int i = 0; i < 8; i++) {
// 不要向左移了又向右移
if (la != -1 && res[i] == la)
continue;
// 记录操作
op[k] = 'A' + i;

// 旋转
rotate(i, st);

if (check(st)) {
op[k + 1] = '\0';
return true;
} else if (IDAstar(k + 1, st, op, i))
return true;

rotate(res[i], st);
}

return false;
}

int main() {
char ch;

while (scanf(" %c", &ch), ch != '0') {
char st[25];
st[0] = ch;
for (int i = 1; i < 24; i++)
scanf(" %c", &st[i]);

depth = 1;

if (check(st)) {
printf("No moves needed\n%c\n", st[6]);
} else {
char op[200];
op[0] = '\0';

// 迭代加深搜索
while (!IDAstar(0, st, op, -1))
depth++;

printf("%s\n%c\n", op, st[6]);
}
}
return 0;
}```

0 条评论

• ### 除以3，乘以2（STL+排序）- Codeforces 997D

Polycarp likes to play with numbers. He takes some integer number x, writes it d...

• ### 独角兽与数列（置换群循环）- HDU 4985

群论是法国数学家伽罗瓦（Galois）的发明。伽罗瓦是一个极具传奇性的人物，年仅21岁就英年早逝于一场近乎自杀的决斗中。他用该理论，具体来说是伽罗瓦群，解决了五...

• ### UVA10305 | 拓扑排序

John has n tasks to do. Unfortunately, the tasks are not independent and the exe...

• ### 有限闭包系统中的极大闭集和半空间分离(CS AI)

研究了抽象闭包系统中闭集和半空间分离的一些算法性质。假设基础闭包系统是有限的，并由相应的闭包算子给出，证明了半空间分离问题是np完全的。与此相反，对于极大闭集分...

• ### ZOJ 3202 Second-price Auction

Time Limit: 1 Second      Memory Limit: 32768 KB

• ### VS.Net 2005 Design-Time Integration

Introduction This article provides an overview of the VS.NET 2005 Design-Time I...

• ### How does it work in Mono's C# compiler?

Introduction Mono is an Open Source free programming language project. It is an ...

• ### CoreOS 配置工具 Ignition v2.2

This pre-release version of the specification is experimental and is subject to ...

• ### WashU EpiGenome Browser使用教程

WashU EpiGenome Browser 是我用过最赞的浏览器，没有之一。希望大伙跟着教程好好学习下！ 还有更多教程见：http://epigenomeg...

• ### poj1611---The Suspects

The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: ...