棘手的模拟, 要算出移动后的位置, 注意顺逆针方向, wa了几次, 用IDA*搜索 因为中心颜色确定,找出每个面与中心颜色不同的数量, 每次旋转最多改变12个, 当不同数量/12>深度时剪枝。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
/*
1 2 3
4 5 6
7 8 9
10 11 12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31 32 33
34 35 36 37 38 39 40 41 42 43 44 45
46 47 48
49 50 51
52 53 54
*/
int n, m, deep;
int ans[10], dir[10];
char a[100];
int e[6][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 22, 23, 24, 34, 35, 36},
{13, 14, 15, 25, 26, 27, 37, 38, 39},
{16, 17, 18, 28, 29, 30, 40, 41, 42},
{19, 20, 21, 31, 32, 33, 43, 44, 45},
{46, 47, 48, 49, 50, 51, 52, 53, 54}};
int center[6] = {5, 23, 26, 29, 32, 50};
int change[12][20] =
{{12, 24, 36, 35, 34, 22, 10, 11, 13, 25, 37, 46, 49, 52, 45, 33, 21, 1, 4, 7},
{10, 11, 12, 24, 36, 35, 34, 22, 1, 4, 7, 13, 25, 37, 46, 49, 52, 45, 33, 21},
{15, 27, 39, 38, 37, 25, 13, 14, 16, 28, 40, 48, 47, 46, 36, 24, 12, 7, 8, 9},
{13, 14, 15, 27, 39, 38, 37, 25, 7, 8, 9, 16, 28, 40, 48, 47, 46, 36, 24, 12},
{18, 30, 42, 41, 40, 28, 16, 17, 19, 31, 43, 54, 51, 48, 39, 27, 15, 9, 6, 3},
{16, 17, 18, 30, 42, 41, 40, 28, 9, 6, 3, 19, 31, 43, 54, 51, 48, 39, 27, 15},
{21, 33, 45, 44, 43, 31, 19, 20, 3, 2, 1, 10, 22, 34, 52, 53, 54, 42, 30, 18},
{19, 20, 21, 33, 45, 44, 43, 31, 42, 30, 18, 3, 2, 1, 10, 22, 34, 52, 53, 54},
{3, 6, 9, 8, 7, 4, 1, 2, 18, 17, 16, 15, 14, 13, 12, 11, 10, 21, 20, 19},
{1, 2, 3, 6, 9, 8, 7, 4, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10},
{48, 51, 54, 53, 52, 49, 46, 47, 40, 41, 42, 43, 44, 45, 34, 35, 36, 37, 38, 39},
{46, 47, 48, 51, 54, 53, 52, 49, 37, 38, 39, 40, 41, 42, 43, 44, 45, 34, 35, 36}};
int geth()
{
int sum = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 9; j++)
if (a[e[i][j]] != a[center[i]])
sum++;
return sum;
}
bool ID_Astar(int d)
{
int h = geth();
if (d + ceil(h / 12.0) > deep)
return false;
if (h == 0)
return true;
char s[100];
for (int i = 0; i < 12; i++)
{
memcpy(s, a, sizeof(a));
for (int j = 0; j < 20; j++)
a[change[i][j]] = s[change[i ^ 1][j]];
ans[d] = i / 2;
if (!(i & 1)) dir[d] = 1;
else dir[d] = -1;
if (ID_Astar(d + 1))
return true;
memcpy(a, s, sizeof(s));
}
return false;
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
for (int i = 1; i <= 54; i++)
cin >> a[i];
if (!geth())
{
printf("0\n");
continue;
}
for (deep = 1; deep <= 5; deep++)
if (ID_Astar(0))
break;
if (deep == 6)
printf("-1\n");
else
{
printf("%d\n", deep);
for (int i = 0; i < deep; i++)
printf("%d %d\n", ans[i], dir[i]);
}
}
return 0;
}