題目描述:c++
The Rotation GameTime Limit: 45000/15000 MS (Java/Others) Memory Limit: 150000/150000 K (Java/Others)
Total Submission(s): 2898 Accepted Submission(s): 1169
api
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 100; const int inf = 0x3f3f3f3f; int center[8] = { 6,7,8,11,12,15,16,17 }; int reveropt[8] = { 5,4,7,6,1,0,3,2 }; int change[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} }; int tmp[24]; int cnt[5]; int get() {//找出8-出現次數最多的數的出現次數,即至少須要移動的次數 int maxcnt = 0; memset(cnt, 0, sizeof(cnt)); for (int i = 0; i < 8; i++) { cnt[tmp[center[i]]]++; maxcnt = max(maxcnt, cnt[tmp[center[i]]]); } return 8 - maxcnt; } void option(int opt) {//移動 int t = tmp[change[opt][0]]; for (int j = 0; j < 6; j++) { tmp[change[opt][j]] = tmp[change[opt][j + 1]]; } tmp[change[opt][6]] = t; } string res; int depth; bool dfs(int step, int lastopt) { int last = get(); if (step + last >= depth) return false;//已經移動+至少須要移動>預設深度 if (!last) {//中心點全都相同了 printf("%s\n", res.c_str()); printf("%d\n", tmp[center[0]]); return true; } for (int i = 0; i < 8; i++) { if (lastopt != -1 && i == reveropt[lastopt])continue;//若是與上次操做恰好相反的就不要 res.push_back('A' + i); option(i);//移動 if (dfs(step + 1, i))return true; option(reveropt[i]);//回溯 res.pop_back(); } return false; } int main() { //freopen("test.txt", "r", stdin); while (~scanf("%d", &tmp[0])) { if (tmp[0] == 0)break; for (int i = 1; i < 24; i++) { scanf("%d", &tmp[i]); } res.clear(); if (!get()) { printf("No moves needed\n%d\n",tmp[center[0]]); continue; } depth = 1; while (1) { if (dfs(0, -1))break; depth++; } } return 0; }