Language: Default The Rotation Game
Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind. Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting Input 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 Output 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 Sample Input 1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 0 Sample Output AC 2 DDHH 2 Source |
题意:如图24个位置上有数字1~3,可以进行移动,每次对一条7个数进行平移,问怎样移动使得中心的8个方格为相同的数字,输出方案和最后中心的数字。
思路:IDA*,有八个操作,主要是这个移动操作不好弄,开一个辅助数组记录移动的位置关系。每移动一次中心改变一个数,以此构造h()。
代码:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define mod 1000000009 const int maxn = 30; const int MAXN = 2005; const int MAXM = 200010; const int N = 1005; int Move[8][14]={ {1,3,3,7,7,12,12,16,16,21,21,23,23,1}, {2,4,4,9,9,13,13,18,18,22,22,24,24,2}, {11,10,10,9,9,8,8,7,7,6,6,5,5,11}, {20,19,19,18,18,17,17,16,16,15,15,14,14,20}, {24,22,22,18,18,13,13,9,9,4,4,2,2,24}, {23,21,21,16,16,12,12,7,7,3,3,1,1,23}, {14,15,15,16,16,17,17,18,18,19,19,20,20,14}, {5,6,6,7,7,8,8,9,9,10,10,11,11,5}, }; int ret[8]={5,4,7,6,1,0,3,2};//用于还原 int a[maxn],index,deep; vector<char>ans; void change(int x) { int y=a[Move[x][0]]; for (int i=0;i<12;i+=2) a[Move[x][i]]=a[Move[x][i+1]]; a[Move[x][12]]=y; } int h(int &x) { int num[4]={0}; for (int i=7;i<=9;i++) num[a[i]]++; for (int i=16;i<=18;i++) num[a[i]]++; num[a[12]]++; num[a[13]]++; int Max=0; for (int i=1;i<=3;i++) { if (Max<num[i]) { Max=num[i]; x=i; } } return 8-Max; } bool dfs(int k) { if (k>deep) return false; int x,y; x=h(y); if (k+x>deep) return false; if (x==0) { index=y; return true; } for (int i=0;i<8;i++) { change(i); ans.push_back(i+'A'); if (dfs(k+1)) return true; ans.pop_back(); change(ret[i]); } return false; } int main() { #ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin); #endif int i,j; while (scanf("%d",&a[1])) { if (a[1]==0) break; for (i=2;i<=24;i++) scanf("%d",&a[i]); int x,y; x=h(y); if (x==0){ printf("No moves needed\n"); printf("%d\n",y); continue; } deep=1; ans.clear(); while (1) { if (dfs(0)) break; deep++; } for (int i=0;i<ans.size();i++) printf("%c",ans[i]); printf("\n%d\n",index); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。