标签:
原题https://uva.onlinejudge.org/external/13/1343.pdf
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int MAXN = 24, LEN = 8; 6 int board[LEN][LEN - 1] = { {0, 2, 6, 11, 15, 20, 22}, {1, 3, 8, 12, 17, 21, 23}, 7 {10, 9, 8, 7, 6, 5, 4}, {19, 18, 17, 16, 15, 14, 13}, 8 {23, 21, 17, 12, 8, 3, 1}, {22, 20, 15, 11, 6, 2, 0}, 9 {13, 14, 15, 16, 17, 18, 19}, {4, 5, 6, 7, 8, 9, 10} }; 10 int check_order[] = {6, 7, 8, 11, 12, 15, 16, 17}, a[MAXN], maxd; 11 char order[30]; 12 13 int unordered() { 14 int n1 = 0, n2 = 0, n3 = 0; 15 for (int i = 0; i < LEN; i++) 16 if (a[check_order[i]] == 1) n1++; 17 else if (a[check_order[i]] == 2) n2++; 18 else n3++; 19 return LEN - max(max(n1, n2), n3); 20 } 21 22 void rotate(int di) { 23 int t = a[board[di][0]]; 24 for (int i = 1; i < LEN - 1; i++) a[board[di][i - 1]] = a[board[di][i]]; 25 a[board[di][LEN - 2]] = t; 26 } 27 28 bool dfs(int d) { 29 int cnt = unordered(); 30 if (!cnt) return true; 31 if (cnt + d > maxd) return false; 32 int temp[MAXN]; memcpy(temp, a, sizeof(a)); 33 for (int i = 0; i < LEN; i++) { 34 rotate(i); 35 order[d] = i + ‘A‘; 36 if (dfs(d + 1)) return true; 37 memcpy(a, temp, sizeof(a)); 38 } 39 return false; 40 } 41 42 int main() { 43 freopen("in", "r", stdin); 44 while (scanf("%d", &a[0]) && a[0]) { 45 for (int i = 1; i < MAXN; i++) scanf("%d", &a[i]); 46 if (!unordered()) { printf("No moves needed\n%d\n", a[6]); continue;} 47 for (maxd = 1;; maxd++) if (dfs(0)) break; 48 for (int i = 0; i < maxd; i++) printf("%c", order[i]); 49 printf("\n%d\n", a[6]); 50 } 51 return 0; 52 }
顺便纪念一下排第六(前面3个是virtual oj......)
Uva1343-The Rotation Game-IDA*算法
标签:
原文地址:http://www.cnblogs.com/Bowen-/p/4955782.html