【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] = '\0';
 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\n");
 99             printf("%d\n", Map[7]);
100             continue;
101         }
102         Lim = 0;
103         while (Lim < 30 && !Dfs(0, -1))
104         {
105             Lim++;
106         }
107         printf("%s\n%d\n", Acts, Ans);
108     }
109 }

 

转载于:https://www.cnblogs.com/lightning34/p/4569825.html

### 关于 POJ 2286 的解决方案 POJ 平台上的题目编号通常对应特定算法或数据结构的应用场景。对于 POJ 2286,虽然未提供具体描述,但从其编号范围推测可能涉及动态规划、贪心策略或其他经典算法。 #### 动态规划方法 如果该问题是关于寻找友好数对的数量,则可以采用如下思路解决: 定义两个数组 `sum_divisors` 和 `friend_pairs` 来分别存储每个数的因子和以及已找到的友好数对数量。通过遍历小于给定上限的所有整数 \( n \),计算它们的因子之和并验证是否存在对应的友好数关系[^2]。 以下是实现这一逻辑的一个 Python 示例代码: ```python def calculate_friend_numbers(limit): sum_divisors = [0] * (limit + 1) # 计算所有数的因子和 for i in range(1, limit + 1): for j in range(i * 2, limit + 1, i): sum_divisors[j] += i friend_count = 0 seen_friends = set() # 验证每一对是否构成友好数字 for num in range(1, limit + 1): partner = sum_divisors[num] if ( partner < limit and partner != num and sum_divisors[partner] == num and (num, partner) not in seen_friends ): friend_count += 1 seen_friends.add((num, partner)) seen_friends.add((partner, num)) # 对称处理 return friend_count print(calculate_friend_numbers(10000)) # 输出小于一万的友好数量 ``` 上述代码实现了基于因子求和的方法来查找指定范围内所有的友好数对,并统计总数。 #### 数据结构优化 如果是类似于 **Balanced Lineup** 这样的区间最值查询问题,则需考虑高效的数据结构支持快速访问操作。例如使用线段树或者稀疏表(Sparse Table),可以在 O(log N) 或更低的时间复杂度下完成多次询问响应[^3]。 假设输入是一系列牛的高度序列,目标是最小化任意连续子列中的最大高度差。那么预处理阶段构建辅助索引之后,在每次请求到来时只需执行常数级运算即可得出结果。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值