zoukankan      html  css  js  c++  java
  • The Rotation Game(IDA*算法)

    The Rotation Game

    Time Limit : 30000/15000ms (Java/Other)   Memory Limit : 300000/150000K (Java/Other)
    Total Submission(s) : 29   Accepted Submission(s) : 12
    Problem 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 //AC代码:
     2 #include <iostream>using namespace std;
     3 int Ms[8][7] ={ 
     4     0, 2, 6,11,15,20,22,                     //A操作对应的那一列在Map中的下标               
     5     1, 3, 8,12,17,21,23,                           //B操作......下同            
     6     10, 9, 8, 7, 6, 5, 4,              
     7     19,18,17,16,15,14,13,            
     8     23,21,17,12, 8, 3, 1,              
     9     22,20,15,11, 6, 2, 0,            
    10     13,14,15,16,17,18,19,             
    11     4, 5, 6, 7, 8, 9,10,
    12 };
    13 int R[8] = {5,4,7,6,1,0,3,2};                              //各操作的相反操作在Ms中的行号
    14 int Mid[8] = {6,7,8,11,12,15,16,17};                //中间8块在Map中的下标
    15 int Map[24],op[50],Depth;
    16 
    17 inline int MAX(int a,int b){    return a > b ? a : b;}
    18 int Value()                                       //估计当前状态到目标状态的最少需要多少操作
    19 {    
    20     int t[3] = {0,0,0};  
    21     for(int i=0;i<8;++i) 
    22         ++t[Map[Mid[i]]-1]; 
    23     return 8-MAX(t[0],MAX(t[1],t[2]));
    24 }
    25 void Move(int k)                               //进行(k+'A')操作
    26 {  
    27     int t = Map[Ms[k][0]];   
    28     for(int i=0;i<6;++i) 
    29         Map[Ms[k][i]] = Map[Ms[k][i+1]];   
    30     Map[Ms[k][6]] = t;
    31 }
    32 bool Dfs(int depth){   
    33     if(depth == Depth) return false;
    34     for(int i=0;i<8;++i) 
    35     {   
    36         Move(i);      
    37         op[depth] = i;     
    38         int v = Value();      
    39         if(v == 0) return true;   
    40         if(depth+v < Depth && Dfs(depth+1)) return true;      
    41         Move(R[i]);   
    42     }    
    43     return false;
    44 }
    45 int main(){ 
    46     while(scanf("%d",&Map[0]),Map[0])    {  
    47         for(int i=1;i<24;++i)
    48             scanf("%d",&Map[i]);    
    49         Depth = Value();     
    50         if(Depth == 0) 
    51             printf("No moves needed
    %d
    ",Map[6]);    
    52         else        
    53         {        
    54             memset(op,0,sizeof(op));    
    55             while(!Dfs(0)) ++Depth;        
    56             for(int i=0;i<Depth;++i) 
    57                 printf("%c",'A'+op[i]);     
    58             printf("
    %d
    ",Map[6]);      
    59         }    
    60     }  
    61     return 0;
    62 }

    没事来膜拜大牛的代码。。。

  • 相关阅读:
    Entity Framework 中Decimal字段长度设置方法
    DWZ框架的使用
    .net mvc之web开发体会
    ASP.NET MVC3.0中同一View如何返回多个Model或数据集
    html5中 viewport 的用法
    swagger-REST API
    Git 添加SSH 取消了输入帐号密码
    [转]并发insert情况下会发生重复的数据插入问题
    如何编写适用于Echarts Map的js文件
    [转]OpenLayers 3 自定义坐标系
  • 原文地址:https://www.cnblogs.com/tt123/p/3411377.html
Copyright © 2011-2022 走看看