zoukankan      html  css  js  c++  java
  • POJ2286 The Rotation Game[IDA*迭代加深搜索]

    The Rotation Game
    Time Limit: 15000MS   Memory Limit: 150000K
    Total Submissions: 6325   Accepted: 2134

    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

    #include<cstdio>
    #include<iostream>
    using namespace std;
    const int N=8;
    int xh[N][N-1]={
        0,2,6,11,15,20,22,
        1,3,8,12,17,21,23,
        10,9,8,7,6,5,4,
        19,18,17,16,15,14,13,
        23,21,17,12,8,3,1,
        22,20,15,11,6,2,0,
        13,14,15,16,17,18,19,
        4,5,6,7,8,9,10
    };
    int mid[8]={6,7,8,11,12,15,16,17};
    int rev[8]={5,4,7,6,1,0,3,2};
    int flag,cnt[4];
    int path[(int)1e5+5];
    char op[10]="ABCDEFGH";
    int s[25];
    inline int h(){
        for(int i=1;i<=3;i++) cnt[i]=0;
        for(int i=0;i<8;i++) cnt[s[mid[i]]]++;
        return 8-max(max(cnt[1],cnt[2]),cnt[3]);
    }
    inline void move(int k){
        int t=s[xh[k][0]];
        for(int i=1;i<7;i++) s[xh[k][i-1]]=s[xh[k][i]];
        s[xh[k][6]]=t;
    }
    inline int check(){
        int v=s[mid[0]];
        for(int i=1;i<8;i++) if(s[mid[i]]!=v) return 0;
        return 1;
    }
    void dfs(int depth,int lim){
        int H=h();
        if(depth+H>lim) return ;
        if(check()){flag=1;return ;}
        for(int i=0;i<8;i++){
            move(i);
            path[depth]=i;
            dfs(depth+1,lim);
            if(flag) return ;
            move(rev[i]);
        }
    }
    int main(){
        while(~scanf("%d",&s[0])&&s[0]){
            for(int i=1;i<24;i++) scanf("%d",&s[i]);
            flag=0;
            for(int i=0;;i++){
                dfs(0,i);
                if(flag){
                    if(i){
                        for(int j=0;j<i;j++) putchar(op[path[j]]);
                        putchar('
    ');
                    }
                    else puts("No moves needed");
                    printf("%d
    ",s[mid[0]]);
                    break;
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    MySQL数据库06 /数据库总结
    数据库05 /索引、数据库备份、锁和事务、慢查询优化、索引命中相关
    数据库04 /多表查询、pymysql模块
    数据库03 /库、表、记录的详细操作、单表查询
    struts基于ognl的自动类型转换需要注意的地方
    struts-json-plugin result中配置对象的序列化
    hibernate关联关系笔记
    使用动态代理实现数据库事务(转)
    xml、文件操作功能类
    将CachedRowSet中的数据转储到对象中
  • 原文地址:https://www.cnblogs.com/shenben/p/6700101.html
Copyright © 2011-2022 走看看