您的位置:首页 > 其它

1343 - The Rotation Game

2015-07-19 13:00 337 查看
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 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

我有话说:

这道题可以转换成一个IDA*算法。估价函数是d+h()>maxd即剪枝。其中h()的计算是分别计算如果中心方格上全是1,或2,或3,实际上与之不同的数字的个数的最小值。因为如果我们往最好的角度想,每次旋转最多可以更新一个正确的数。一进一出。进来一个正确的,出去一个不正确的。所以至少还要旋转h()次。还有学到的就是编号的好处。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
/*    A     B
00    01
02    03
H 04 05 06 07 08 09 10 C
11    12
G 13 14 15 16 17 18 19 D
20    21
22    23
F      E
*/
int line[8][7]={{ 0, 2, 6,11,15,20,22},//A
{ 1, 3, 8,12,17,21,23},//B
{10, 9, 8, 7, 6, 5, 4},//C
{19,18,17,16,15,14,13},//D};
int rev[8]={5,4,7,6,1,0,3,2};
int certern[8]={6,7,8,11,12,15,16,17};
int a[24];
char ans[1000];
bool is_final(){
for(int i=0;i<8;i++)
if(a[certern[i]]!=a[certern[0]])return false;
return true;
}
int diff(int target)
{
int ans=0;
for(int i=0;i<8;i++)
{
if(a[certern[i]]!=target)ans++;
}
return ans;
}
inline int h()
{
return min(diff(1),min(diff(2),diff(3)));
}
inline void move(int i)
{
int temp=a[line[i][0]];
for(int j=0;j<6;j++)
{
a[line[i][j]]=a[line[i][j+1]];
}
a[line[i][6]]=temp;
}

bool dfs(int d,int maxd){
if(is_final()){
ans[d]='\0';
printf("%s\n",ans);
return true;
}
if(d+h()>maxd)return false;
for(int i=0;i<8;i++)
{
ans[d]='A'+i;
move(i);
if(dfs(d+1,maxd))return true;//
move(rev[i]);
}
return false;
}
int main()
{
for(int i=4;i<8;i++)
for(int j=0;j<7;j++)line[i][j]=line[rev[i]][6-j];
//借助rev[i]得到E F G H
while(scanf("%d",&a[0])==1&&a[0]){
for(int i=1;i<24;i++)scanf("%d",&a[i]);
for(int i=0;i<24;i++)if(!a[i]){return 0;}
if(is_final()){
printf("No moves needed\n");
}else{
for(int maxd=1;;maxd++)//IDA*
if(dfs(0,maxd))break;
}
printf("%d\n",a[6]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: