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*的估值函数用在哪呢?
回顾一下A*算法,A*算法其实是BFS (优先队列 估值函数)。而估值函数如下图所示:
其中g(x)是已经消耗的值,h(x)是估算的到终点的值,这个值往往非常重要,也是难点。
IDA*算法则是DFS 迭代加深搜索 估值函数,迭代加深比较容易理解,也就是在搜索过程中记录搜索的深度,如果超过深度就不继续搜索。注意DFS算法基本上都是需要进行回溯还原现场处理的。比如本题中的旋转操作,每一次旋转,在搜索完成时都会还原这个旋转操作。估值函数在DFS中往往用于剪枝操作,本题中将剩余数字作为估值函数,判断是否需要继续搜索。
源代码:
代码语言:javascript复制#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] = '