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

    The Rotation Game

    Time Limit: 15000MS

     

    Memory Limit: 150000K

    Total Submissions: 5691

     

    Accepted: 1918

    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

    Source

    Shanghai 2004

    【思路】

      IDA*算法。

      IDA*=ID-DFS+A*,即不断加深搜索的深度并设计一个乐观估计函数进行剪枝。

      应用与本题中;

        乐观估计:设中心8个数字最小不相同的数目为h,则至少需要dep+h个操作才能使得满足条件(dep为当前深度)。

        迭代加深:不断增加操作数的数目,只要有符合的则中断迭代。

      参考了紫书的附加代码,其中的hash有效简化了程序。

    【代码】

     1 #include<cstdio>
     2 #include<string>
     3 #include<cstring>
     4 #include<vector>
     5 #include<iostream>
     6 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
     7 using namespace std;
     8 
     9     int hash[8][7]={
    10                         {0,2,6,11,15,20,22},
    11                         {1,3,8,12,17,21,23},
    12                         {10,9,8,7,6,5,4},
    13                         {19,18,17,16,15,14,13} };
    14 const int rev[8]={5,4,7,6,1,0,3,2};
    15 const int certain[8]={6,7,8,11,12,15,16,17};
    16 
    17 int a[30];
    18 char ans[1010];
    19 
    20 int diff(int c) {
    21     int cnt=0;
    22     FOR(i,0,8) if(c!=a[certain[i]]) cnt++;
    23     return cnt;
    24 }
    25 
    26 int h() {
    27     return min(min(diff(1),diff(2)),diff(3));
    28 }
    29 
    30 bool is_final() {
    31     FOR(i,0,8) if(a[certain[0]]!=a[certain[i]]) return false;
    32     return true;
    33 }
    34 
    35 void move(int i) {
    36     int tmp=a[hash[i][0]];
    37     FOR(j,0,6) a[hash[i][j]]=a[hash[i][j+1]];
    38     a[hash[i][6]]=tmp;
    39 }
    40 
    41 bool IDDFS(int dep,int max_d) {
    42     if(is_final()) {
    43         ans[dep] = '';
    44         cout<<ans<<"
    ";
    45         return true;
    46     }
    47     if(dep+h()>max_d) return false;
    48     FOR(i,0,8) {
    49         ans[dep] = 'A'+i;
    50         move(i);
    51         if(IDDFS(dep+1,max_d)) return true;
    52         move(rev[i]);
    53     }
    54     return false;
    55 }
    56 
    57 int main() 
    58 {
    59     FOR(i,4,8) {
    60         FOR(j,0,7) hash[i][j]=hash[rev[i]][6-j];
    61     }
    62     while(scanf("%d",&a[0]) && a[0])
    63     {
    64         FOR(i,1,24) scanf("%d",&a[i]);
    65         FOR(i,0,24) if(!a[i]) return 0;
    66         if(is_final()) 
    67            printf("No moves needed
    ");
    68         else {
    69             for(int max_d=1; ;max_d++)
    70                if(IDDFS(0,max_d)) break;
    71         }
    72         printf("%d
    ",a[6]);
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    http编程中的get和post混合使用方式
    SQLServer实现作业依赖(非步骤)
    SQLServer实现两个库的字段长度自动更新
    Python+SQLite数据库实现服务端高并发写入
    sqlite数据库相关使用
    sqlite语法
    VBA关键字总结
    VS2005 .net2.0 TreeView.设置SelectedNodeStyle控制TreeView中选定节点的外观的
    SQLSERVER 2005 如何给sa用户设置空密码?
    解决超过远程连接数而无法连接服务器的问题 踢出已断开用户
  • 原文地址:https://www.cnblogs.com/lidaxin/p/4928695.html
Copyright © 2011-2022 走看看