• UVA_Rotation Game<旋转游戏> UVA 1343


    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*,启发函数就是每次最多只能恢复一个...

    代码比较挫(旋转那完全是人工。。。)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int s[24];
    int maxd;
    char ans[150];
    
    bool input(){
    scanf("%d",&s[0]);
    if(s[0] == 0) return false;
    for(int i = 1;i<=23;++i)
     scanf("%d",&s[i]);
    return true;    
    }
    
    
    void ope(int i,int * d){
    int ch;
    if (i == 0)
     {
      ch = d[0];
      d[0] = d[2];
      d[2] = d[6];
      d[6] = d[11];
      d[11] = d[15];
      d[15] = d[20];
      d[20] = d[22];
      d[22] = ch;
     }
    else if(i == 1)
     {
       ch = d[1];
       d[1] = d[3];
       d[3] = d[8];
       d[8] = d[12];
       d[12] = d[17];
       d[17] = d[21];
       d[21] = d[23];
       d[23] = ch;    
     }
    else if(i == 2)
     {
       ch = d[10];
       for(int i = 10;i>=5;--i)
        d[i] = d[i-1];
        d[4] = ch;
     }
    else if(i == 3)
     {
       ch = d[19];
       for(int i = 19;i>=14;--i)
        d[i] = d[i-1];
        d[13] = ch;    
     }
    else if(i == 4)
     {
       ch = d[23];
       d[23] = d[21];
       d[21] = d[17];
       d[17] = d[12];
       d[12] = d[8];
       d[8] = d[3];
       d[3] = d[1];
       d[1] = ch;
     }
    else if(i == 5)
     {
         ch = d[22];
         d[22] = d[20];
         d[20] = d[15];
         d[15] = d[11];
         d[11] = d[6];
         d[6]= d[2];
         d[2] = d[0];
         d[0] = ch;
     }
    else if(i == 6)
     {
       ch = d[13];
       for(int i = 13;i<=18;++i)
        d[i] = d[i+1];
       d[19] = ch;    
     }
    else if(i == 7)
     {
       ch = d[4];
       for(int i = 4;i<=9;++i)
        d[i] = d[i+1];
       d[10] = ch;    
     }
        
    }
    
    
    bool dfs(int d,int *t,int tid){
    if (d == maxd)
    {
      if (t[11] != tid || t[12] != tid) return false;
      for(int i = 6;i<=8;++i)
       if(t[i] != tid)
        return false;
      for(int i = 15;i<=17;++i)
       if(t[i] != tid)
        return false;
      return true;
    }
    int co = 0;
    if (t[11] != tid ) co ++;
    if (t[12] != tid) co ++;
    for(int i = 6;i<=8;++i)
     if(t[i] != tid)
       co ++;
    for(int i = 15;i<=17;++i)
     if(t[i] != tid)
      co ++;
    if (co + d > maxd) return false;
    for(int i = 0;i<8;++i)
     {
         int nt[24];
         memcpy(nt,t,sizeof(nt));
         ope(i,nt);
         ans[d] = i + 'A';
         if (dfs(d+1,nt,tid)) return true;
     }
    
    return false;    
    }
    
    
    
    
    int main(int argc,char * argv[]){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    int targetid,outid;
    char outans[150];
    while(input())
    {
      for(int i = 0;;++i)
       {
           int ok = 0;
           maxd = i ;
           for(int j = 1;j<=3;++j)
                if (dfs(0,s,j))
                 {
                     ok++;
                     if (ok == 1) 
                     {
                         outid = j;
                     memcpy(outans,ans,sizeof(outans));
                     }
    
                     if (ok >= 2)
                      {
                          int check = 0;
                          for(int i = 0;i<maxd;++i)
                           if(outans[i] > ans[i])
                            {
                                check = 1;
                               break;
                            }
                            else if(outans[i] < ans[i])
                             break;
                         if(check)
                          {
                              memcpy(outans,ans,sizeof(outans));
                              
                              outid = j;
                          }
                          
                      }
                 }
        if(ok)
         break;
           
       }
      if (maxd == 0)
       cout << "No moves needed" << endl << outid << endl;
      else
       {
           outans[maxd] = '';
           cout << outans << endl << outid << endl;
       }    
    }
    
    
    return 0;    
    }
    No Pain , No Gain.
  • 相关阅读:
    02-链路层
    01-TCP/IP概述
    ARM Cortex-A9 (tiny 4412)
    STM32 f407 温湿度采集报警
    arduino mega 避障报距小车
    归纳法调试
    python 数据类型Ⅲ(字典)
    Python 数据类型Ⅱ(列表,元祖)
    Python 数据类型(str,int,bool)
    Python while循环&格式化输出&运算符
  • 原文地址:https://www.cnblogs.com/Xiper/p/4467797.html
Copyright © 2020-2023  润新知