POJ 2286 The Rotation Game

来源:互联网 发布:数据库管理系统DBMSS 编辑:程序博客网 时间:2024/06/06 04:13

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 of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration. 

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 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. 

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 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. 

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 31 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 30

Sample Output

AC2DDHH2

Source

Shanghai 2004

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

IDA*~

感觉估价函数会对运行的时间产生很大影响……像我这个就限制的很宽,不过居然能过也是很神奇啊~

具体就是迭代加深搜索+估价,看起来很麻烦实际上画个图就很好写了~


#include<cstdio>#include<cstring>#include<iostream>using namespace std;int a[25],ans,che[8]={7,8,9,12,13,16,17,18},sol[120],chan[8][7]={{1,3,7,12,16,21,23},{2,4,9,13,18,22,24},{11,10,9,8,7,6,5},{20,19,18,17,16,15,14},{24,22,18,13,9,4,2},{23,21,16,12,7,3,1},{14,15,16,17,18,19,20},{5,6,7,8,9,10,11}},cas[8]={5,4,7,6,1,0,3,2};bool flag=0;int chec(){for(int i=1;i<8;i++)  if(a[che[0]]!=a[che[i]]) return 0;return 1;}int cal(){int minn=999999999,tot;for(int i=1;i<=3;i++){tot=0;for(int j=0;j<8;j++) if(a[che[j]]!=i) tot++;minn=min(tot,minn);}return minn;}void cha(int u){for(int i=1;i<=6;i++) swap(a[chan[u][i]],a[chan[u][i-1]]);}void findd(int u){if(chec()){flag=1;return;}if(cal()+u>ans) return;for(int i=0;i<8;i++){cha(i);sol[u]=i;findd(u+1);if(flag) return;cha(cas[i]);}}int main(){while(scanf("%d",&a[1])==1 && a[1]){for(int i=2;i<=24;i++) scanf("%d",&a[i]);if(chec()){printf("No moves needed\n%d\n",a[7]);continue;}for(flag=0,ans=1;;ans++){findd(0);if(flag) break;}for(int i=0;i<ans;i++) printf("%c",sol[i]+'A');printf("\n%d\n",a[7]);}return 0;}


1 0
原创粉丝点击