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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| #include <bits/stdc++.h>
std::map<std::string, int> corner{ {"BRY", 0}, {"YRG", 1}, {"GRW", 2}, {"WRB", 3}, {"BOW", 4}, {"WOG", 5}, {"GOY", 6}, {"YOB", 7} };
using Perm = std::array<int, 24>;
const Perm rot{2, 0, 1, 10, 11, 9, 14, 12, 13, 22, 23, 21, 20, 18, 19, 16, 17, 15, 8, 6, 7, 4, 5, 3};
Perm twist[6]{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 16, 17, 18, 19, 20, 21, 22, 23, 12, 13, 14}, {0, 1, 2, 3, 4, 5, 10, 11, 9, 14, 12, 13, 16, 17, 15, 8, 6, 7, 18, 19, 20, 21, 22, 23}, {0, 1, 2, 7, 8, 6, 17, 15, 16, 9, 10, 11, 12, 13, 14, 19, 20, 18, 5, 3, 4, 21, 22, 23} };
Perm mul(const Perm &a, const Perm &b) { Perm c; for (int i = 0; i < 24; ++i) c[i] = a[b[i]]; return c; }
Perm inv(const Perm &a) { Perm b; for (int i = 0; i < 24; ++i) b[a[i]] = i; return b; }
Perm get(char s[24]) { Perm a; for (int i = 0; i < 24; i += 3) { for (int j = 0; j < 3; ++j) { auto t = std::string({s[i + j], s[i + (j + 1) % 3], s[i + (j + 2) % 3]}); if (corner.count(t)) { int v = corner[t]; a[i + j] = 3 * v; a[i + (j + 1) % 3] = 3 * v + 1; a[i + (j + 2) % 3] = 3 * v + 2; } } } int k = (std::find(a.begin(), a.end(), 0) - a.begin()) / 3; if (k >= 4) { for (int i = 0; i < 12; ++i) std::swap(a[i], a[i + 12]); k -= 4; } Perm temp; for (int i = 0; i < 12; ++i) temp[i] = a[(i + 3 * k) % 12]; for (int i = 0; i < 12; ++i) temp[12 + (i + 3 * k) % 12] = a[12 + i]; a = std::move(temp); while (a[0] != 0) a = mul(a, rot); return a; }
int fac[7];
int hash(const Perm &a) { int res = 0; int x = (1 << 7) - 1; for (int i = 0; i < 7; ++i) { int v = a[3 * i + 3] / 3 - 1; res += fac[6 - i] * __builtin_popcount(x & ((1 << v) - 1)); x ^= 1 << v; } for (int i = 1; i < 7; ++i) { int v = a[3 * i] % 3; res = 3 * res + v; } return res; }
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); fac[0] = 1; for (int i = 1; i < 7; ++i) fac[i] = i * fac[i - 1]; for (int i = 0; i < 3; ++i) twist[i + 3] = inv(twist[i]);
constexpr int N = 3674160; int dis[N]; std::fill(dis, dis + N, -1); Perm a; std::iota(a.begin(), a.end(), 0); dis[hash(a)] = 0; std::queue<Perm> que; que.push(a); while (!que.empty()) { auto u = que.front(); que.pop(); int d = dis[hash(u)]; for (int i = 0; i < 6; ++i) { auto v = mul(u, twist[i]); int h = hash(v); if (dis[h] == -1) { dis[h] = d + 1; que.push(v); } } } int t; std::cin >> t; while (t--) { char a[24], b[24], foo; std::cin >> a[20] >> a[3] >> foo >> b[20] >> b[3]; std::cin >> a[21] >> a[2] >> foo >> b[21] >> b[2]; std::cin >> a[19] >> a[22] >> a[23] >> a[0] >> a[1] >> a[4] >> a[5] >> a[18] >> foo >> b[19] >> b[22] >> b[23] >> b[0] >> b[1] >> b[4] >> b[5] >> b[18]; std::cin >> a[16] >> a[13] >> a[12] >> a[11] >> a[10] >> a[7] >> a[6] >> a[17] >> foo >> b[16] >> b[13] >> b[12] >> b[11] >> b[10] >> b[7] >> b[6] >> b[17]; std::cin >> a[14] >> a[9] >> foo >> b[14] >> b[9]; std::cin >> a[15] >> a[8] >> foo >> b[15] >> b[8]; auto u = get(a), v = get(b); v = mul(inv(u), v); std::cout << dis[hash(v)] << "\n"; } return 0; }
|