Problem UVA1434-The Rotation Gameios
The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single ‘0’ after the last test case that ends the input.算法
For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from ‘A’ to ‘H’, and there should not be any spaces between the letters in the line. If no moves are needed, output ‘No moves needed’ instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.數組
AC 框架
2 spa
DDHH code
2blog
題解:作了兩道IDA*的題,對這個算法有了一個初步的印象,感受挺強大的,並且代碼短,思路清晰。ip
這個題搜索的框架很簡單(IDA*的好處),可是具體實現起來不太容易。第一個技巧,用靜態數組來搞定旋轉的問題,第二個技巧,使用rev數組來精簡代碼。input
rev數組不僅是節省了一些代碼量,並且能夠輕鬆實現回溯時數組的復原,不然還要先把原數組存到一個另外一個數組裏,回溯時再copy回來,省了空間和時間,很是巧妙。string
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 using namespace std; 8 9 /* 10 00 01 11 02 03 12 04 05 06 07 08 09 10 13 11 12 14 13 14 15 16 17 18 19 15 20 21 16 22 23 17 */ 18 19 const int maxn = 24; 20 int num[maxn]; 21 char ans[10000]; 22 int maxd; 23 24 int line[8][7] = 25 { 26 { 0, 2, 6,11,15,20,22}, 27 { 1, 3, 8,12,17,21,23}, 28 {10, 9, 8, 7, 6, 5, 4}, 29 {19,18,17,16,15,14,13}, 30 }; 31 32 const int rev[8] = { 5,4,7,6,1,0,3,2 }; 33 const int center[8] = { 6,7,8,11,12,15,16,17 }; 34 35 bool is_ok() { 36 int t = num[center[0]]; 37 for (int i = 1; i < 8; i++) { 38 if (num[center[i]] != t) return false; 39 } 40 return true; 41 } 42 43 int cal(int tar) { 44 int cnt = 0; 45 for (int i = 0; i < 8; i++) { 46 if (num[center[i]] != tar) cnt++; 47 } 48 return cnt; 49 } 50 51 inline int h() { 52 return min(min(cal(1), cal(2)), cal(3)); 53 } 54 55 void move(int i) { 56 int tmp = num[line[i][0]]; 57 for (int j = 0; j < 6; j++) { 58 num[line[i][j]] = num[line[i][j + 1]]; 59 } 60 num[line[i][6]] = tmp; 61 } 62 63 bool dfs(int d) { 64 if (is_ok()) { 65 ans[d] = '\0'; 66 printf("%s\n", ans); 67 return true; 68 } 69 if (d + h() > maxd) { 70 return false; 71 } 72 for (int i = 0; i < 8; i++) { 73 ans[d] = 'A' + i; 74 move(i); 75 if (dfs(d + 1)) { 76 return true; 77 } 78 move(rev[i]); 79 } 80 return false; 81 } 82 83 int main() 84 { 85 //freopen("input.txt", "r", stdin); 86 //freopen("output.txt", "w", stdout); 87 for (int i = 4; i < 8; i++) { 88 for (int j = 0; j < 7; j++) { 89 line[i][j] = line[rev[i]][6 - j]; 90 } 91 } 92 while (scanf("%d", &num[0]) != -1 && num[0]) { 93 for (int i = 1; i < 24; i++) { 94 scanf("%d", &num[i]); 95 } 96 for (int i = 0; i < 24; i++) { 97 if (!num[i]) return 0; 98 } 99 100 if (is_ok()) { 101 printf("No moves needed\n"); 102 } 103 else { 104 for (maxd = 1;; maxd++) { 105 if (dfs(0)) break; 106 } 107 } 108 printf("%d\n", num[center[0]]); 109 } 110 return 0; 111 }