zoukankan      html  css  js  c++  java
  • 【POJ2286】The Rotation Game

    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
    

     大意:

      将如图所示的棋盘按照A-H的方式滚动,最后使得中间8个相同,求移动次数最少的方案和最终中间的数。

    分析:

      直接搜索肯定不行,用启发式搜索,迭代深搜。

      1 #include <cstdio>
      2 
      3 int R[8][7] = 
      4 {
      5     {1, 3, 7, 12, 16, 21, 23},
      6     {2, 4, 9, 13, 18, 22, 24},
      7     {11, 10, 9, 8, 7, 6, 5},
      8     {20, 19, 18, 17, 16, 15, 14},
      9     {24, 22, 18, 13, 9, 4, 2},
     10     {23, 21, 16, 12, 7, 3, 1},
     11     {14, 15, 16, 17, 18, 19, 20},
     12     {5, 6, 7, 8, 9, 10, 11}
     13 };
     14 
     15 int Judge[8] = 
     16 {
     17     7, 8, 9, 12, 13, 16, 17, 18
     18 };
     19 
     20 int Opp[8] = 
     21 {
     22     5, 4, 7, 6, 1, 0, 3, 2
     23 };
     24 
     25 int Map[25];
     26     
     27 inline void Rotate(int x)
     28 {
     29     for (int i = 0; i < 6; i++)
     30     {
     31         Map[R[x][i]] ^= Map[R[x][i + 1]];
     32         Map[R[x][i + 1]] ^= Map[R[x][i]];
     33         Map[R[x][i]] ^= Map[R[x][i + 1]];
     34     }
     35 }
     36 
     37 int Lim, Ans;
     38 
     39 int Cnt[5];
     40 
     41 char Acts[31];
     42 
     43 inline int Min(int a, int b)
     44 {
     45     return a < b ? a : b;
     46 }
     47 
     48 inline int Mark()
     49 {
     50     Cnt[1] = Cnt[2] = Cnt[3] = 8;
     51     for (int i = 0; i < 8; i++)
     52     {
     53         Cnt[Map[Judge[i]]]--;
     54     }
     55     return Min(Cnt[1], Min(Cnt[2], Cnt[3]));
     56 }
     57 
     58 bool Dfs(int Depth, int Skip)
     59 {
     60     int M = Mark();
     61     if (!M)
     62     {
     63         Ans = Map[7];
     64         Acts[Depth] = '';
     65         return 1;
     66     }
     67     if (M + Depth > Lim)
     68     {
     69         return 0;
     70     }
     71     for (int i = 0; i < 8; i++)
     72     {
     73         if (i == Skip)
     74         {
     75             continue;
     76         }
     77         Rotate(i);
     78         if (Dfs(Depth + 1, Opp[i]))
     79         {
     80             Acts[Depth] = 'A' + i;
     81             return 1;
     82         }
     83         Rotate(Opp[i]);
     84     }
     85     return 0;
     86 }
     87 
     88 int main()
     89 {
     90     while (scanf("%d", &Map[1]) && Map[1])
     91     {
     92         for (int i = 2; i < 25; i++)
     93         {
     94             scanf("%d", &Map[i]);
     95         }
     96         if (Mark() == 0)
     97         {
     98             printf("No moves needed
    ");
     99             printf("%d
    ", Map[7]);
    100             continue;
    101         }
    102         Lim = 0;
    103         while (Lim < 30 && !Dfs(0, -1))
    104         {
    105             Lim++;
    106         }
    107         printf("%s
    %d
    ", Acts, Ans);
    108     }
    109 }
  • 相关阅读:
    Playwright安装及基本用法
    生成随机数、随机字符串
    xmind2testcase使用
    jmeter5.0二次开发环境搭建(IDEA)
    pytest配置文件pytest.ini
    pytest+allure2生成测试报告
    pytest生成html报告-使用pytest-html插件方式
    pytest一些简单参数
    pytest简单搭建和入门
    python3学习-元组
  • 原文地址:https://www.cnblogs.com/lightning34/p/4569825.html
Copyright © 2011-2022 走看看