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
| #include<bits/stdc++.h> using namespace std;
int opposite[8] = { 5, 4, 7, 6, 1, 0, 3, 2 }; int maxdepth; int line[8][7] = { { 0, 2, 6, 11, 15, 20, 22 }, { 1, 3, 8, 12, 17, 21, 23 }, { 10, 9, 8, 7, 6, 5, 4 }, { 19, 18, 17, 16, 15, 14, 13 }, { 23, 21, 17, 12, 8, 3, 1 }, { 22, 20, 15, 11, 6, 2, 0 }, { 13, 14, 15, 16, 17, 18, 19 }, { 4, 5, 6, 7, 8, 9, 10 } }; string ans; int circle[8] = { 6,7,8,11,12,15,16,17 }; int board[24];
int estimate() { int cnt[4] = { 0 }, ans = 0; for (int i = 0; i<8; ++i) ans = max(ans, ++cnt[board[circle[i]]]); return 8 - ans; }
void doit(int op) { int temp = board[line[op][0]]; for (int i = 0; i<6; ++i) board[line[op][i]] = board[line[op][i + 1]]; board[line[op][6]] = temp; }
bool dfs(int depth) { int guess = estimate(); if (guess == 0) return true; else if (depth + guess>maxdepth) return false; for (int i = 0; i<8; ++i) { doit(i); ans.push_back('A' + i); if (dfs(depth + 1)) return true; ans.pop_back(); doit(opposite[i]); } return false; }
void ida() { for (int i = 1;; ++i) { maxdepth = i; if (dfs(0)) break; } }
int main() { while (cin >> board[0], board[0]) { ans.clear(); for (int i = 1; i<24; ++i) cin >> board[i]; if (estimate() == 0) { cout << "No moves needed" << endl; cout << board[7] << endl; } else { ida(); cout << ans << endl; cout << board[7] << endl; } } }
|