POJ 2286 The Rotation Game

The Rotation Game
Time Limit: 15000MS Memory Limit: 150000K
Total Submissions: 4983 Accepted: 1655

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


        题意:两行两列数(只能是1~3),组成一个“井”字型棋盘,每次操作能循环移动其中一行或一列,目标是让棋盘中心的数相同。输出移动方案(步数最小,字典序最小)。

        思路:IDA* 搜索

        这是本人第一篇博客,它也是有故事的。。因为本人喜欢玩魔方,喜欢写程序,就一直想写解魔方的程序。百度之,用IDA*,高大上,不懂。。。后来加入了ACM,就一直对搜索题情有独钟,经过一段时间,终于AC了第一道用IDA*的题目。

        个人感觉,这个IDA*就是一个经过强力剪枝的DFS,能够提前“预判”到没有前途的路径,把它舍弃掉。在这个程序中,get_h()用于预估当前状态与目标态的距离,既不能高估又必须尽可能准确。估价函数起到了非常重要的作用,如果没有一个有效的估价函数,IDA*也就没有了意义。

        偷了点懒,没有写判重,不过似乎不影响。废话不说,上代码:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <cmath>  
  4. #include <algorithm>  
  5. #include <iomanip>  
  6. #include <cstdlib>  
  7. #include <string>  
  8. #include <memory.h>  
  9. #include <vector>  
  10. #include <queue>  
  11. #include <stack>  
  12. #include <ctype.h>  
  13. using namespace std;  
  14.   
  15. int puzzle [8][8];  
  16.   
  17. int get_h(){  
  18.     int a=0;  
  19.     int b=0;  
  20.     int c=0;  
  21.     for(int i=3;i<6;i++){  
  22.         for(int j=3;j<6;j++){  
  23.             if(puzzle[i][j]==1)a++;  
  24.             if(puzzle[i][j]==2)b++;  
  25.             if(puzzle[i][j]==3)c++;  
  26.         }  
  27.     }  
  28.     int ans=max(a,b);  
  29.     ans=max(ans,c);  
  30.     return 8-ans;  
  31. }  
  32.   
  33. void input(){  
  34.     scanf("%d",&puzzle[1][5]);  
  35.     scanf("%d",&puzzle[2][3]);  
  36.     scanf("%d",&puzzle[2][5]);  
  37.     for(int i=1;i<=7;i++){  
  38.         scanf("%d",&puzzle[3][i]);  
  39.     }  
  40.     scanf("%d",&puzzle[4][3]);  
  41.     scanf("%d",&puzzle[4][5]);  
  42.     for(int i=1;i<=7;i++){  
  43.         scanf("%d",&puzzle[5][i]);  
  44.     }  
  45.     scanf("%d",&puzzle[6][3]);  
  46.     scanf("%d",&puzzle[6][5]);  
  47.     scanf("%d",&puzzle[7][3]);  
  48.     scanf("%d",&puzzle[7][5]);  
  49. }  
  50.   
  51. void A(){  
  52.     int tmp=puzzle[1][3];  
  53.     for(int i=1;i<7;i++){  
  54.         puzzle[i][3]=puzzle[i+1][3];  
  55.     }  
  56.     puzzle[7][3]=tmp;  
  57. }  
  58. void B(){  
  59.     int tmp=puzzle[1][5];  
  60.     for(int i=1;i<7;i++){  
  61.         puzzle[i][5]=puzzle[i+1][5];  
  62.     }  
  63.     puzzle[7][5]=tmp;  
  64. }  
  65. void C(){  
  66.     int tmp=puzzle[3][7];  
  67.     for(int i=7;i>1;i--){  
  68.         puzzle[3][i]=puzzle[3][i-1];  
  69.     }  
  70.     puzzle[3][1]=tmp;  
  71. }  
  72. void D(){  
  73.     int tmp=puzzle[5][7];  
  74.     for(int i=7;i>1;i--){  
  75.         puzzle[5][i]=puzzle[5][i-1];  
  76.     }  
  77.     puzzle[5][1]=tmp;  
  78. }  
  79. void E(){  
  80.     int tmp=puzzle[7][5];  
  81.     for(int i=7;i>1;i--){  
  82.         puzzle[i][5]=puzzle[i-1][5];  
  83.     }  
  84.     puzzle[1][5]=tmp;  
  85. }  
  86. void F(){  
  87.     int tmp=puzzle[7][3];  
  88.     for(int i=7;i>1;i--){  
  89.         puzzle[i][3]=puzzle[i-1][3];  
  90.     }  
  91.     puzzle[1][3]=tmp;  
  92. }  
  93. void G(){  
  94.     int tmp=puzzle[5][1];  
  95.     for(int i=1;i<7;i++){  
  96.         puzzle[5][i]=puzzle[5][i+1];  
  97.     }  
  98.     puzzle[5][7]=tmp;  
  99. }  
  100. void H(){  
  101.     int tmp=puzzle[3][1];  
  102.     for(int i=1;i<7;i++){  
  103.         puzzle[3][i]=puzzle[3][i+1];  
  104.     }  
  105.     puzzle[3][7]=tmp;  
  106. }  
  107.   
  108. bool judge(){  
  109.     if( !(puzzle[3][3]-puzzle[3][4]) && !(puzzle[3][4]-puzzle[3][5]) && !(puzzle[3][4]-puzzle[3][5]) &&  
  110.     !(puzzle[3][5]-puzzle[4][5]) && !(puzzle[4][5]-puzzle[5][5]) && !(puzzle[5][5]-puzzle[5][4]) &&  
  111.     !(puzzle[5][4]-puzzle[5][3]) && !(puzzle[5][3]-puzzle[4][3]) ) {  
  112.         return true;  
  113.     }  
  114.     return false;  
  115. }  
  116.   
  117. char ans[100];  
  118. int depth=0;  
  119.   
  120. bool DFS(int d){  
  121.     if(d+get_h()>depth)return false;  
  122.     if(depth==d){  
  123.         if(judge()){  
  124.             return true;  
  125.         }else{  
  126.             return false;  
  127.         }  
  128.     }  
  129.   
  130.     A();  
  131.     if(DFS(d+1)){ans[d]='A';return true;}  
  132.     F();  
  133.       
  134.     B();  
  135.     if(DFS(d+1)){ans[d]='B';return true;}  
  136.     E();  
  137.       
  138.     C();  
  139.     if(DFS(d+1)){ans[d]='C';return true;}  
  140.     H();  
  141.       
  142.     D();  
  143.     if(DFS(d+1)){ans[d]='D';return true;}  
  144.     G();  
  145.       
  146.     E();  
  147.     if(DFS(d+1)){ans[d]='E';return true;}  
  148.     B();  
  149.       
  150.     F();  
  151.     if(DFS(d+1)){ans[d]='F';return true;}  
  152.     A();  
  153.       
  154.     G();  
  155.     if(DFS(d+1)){ans[d]='G';return true;}  
  156.     D();  
  157.       
  158.     H();  
  159.     if(DFS(d+1)){ans[d]='H';return true;}  
  160.     C();  
  161.       
  162.     return false;  
  163. }  
  164.   
  165. int main(){  
  166.     memset(puzzle,0,sizeof(puzzle));  
  167.     while(scanf("%d",&puzzle[1][3])){  
  168.         depth=0;  
  169.         memset(ans,0,sizeof(ans));  
  170.         if(!puzzle[1][3])break;  
  171.         input();  
  172.         while(!DFS(0)){  
  173.             depth++;  
  174.         }  
  175.         if(depth==0){  
  176.             printf("No moves needed\n");  
  177.         }else{  
  178.             printf("%s\n",ans);  
  179.         }  
  180.         printf("%d\n",puzzle[3][3]);  
  181.     }  
  182.     return 0;  
  183. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值