POJ 2286 The Rotation Game

本文介绍使用IDA*搜索算法解决旋转游戏问题的方法。通过合理评估函数预估状态距离,实现最少步骤使棋盘中心符号统一。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Rotation Game
Time Limit: 15000MS Memory Limit: 150000K
Total Submissions: 4983 Accepted: 1655

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
 


        题意:两行两列数(只能是1~3),组成一个“井”字型棋盘,每次操作能循环移动其中一行或一列,目标是让棋盘中心的数相同。输出移动方案(步数最小,字典序最小)。

        思路:IDA* 搜索

        这是本人第一篇博客,它也是有故事的。。因为本人喜欢玩魔方,喜欢写程序,就一直想写解魔方的程序。百度之,用IDA*,高大上,不懂。。。后来加入了ACM,就一直对搜索题情有独钟,经过一段时间,终于AC了第一道用IDA*的题目。

        个人感觉,这个IDA*就是一个经过强力剪枝的DFS,能够提前“预判”到没有前途的路径,把它舍弃掉。在这个程序中,get_h()用于预估当前状态与目标态的距离,既不能高估又必须尽可能准确。估价函数起到了非常重要的作用,如果没有一个有效的估价函数,IDA*也就没有了意义。

        偷了点懒,没有写判重,不过似乎不影响。废话不说,上代码:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <cmath>  
  4. #include <algorithm>  
  5. #include <iomanip>  
  6. #include <cstdlib>  
  7. #include <string>  
  8. #include <memory.h>  
  9. #include <vector>  
  10. #include <queue>  
  11. #include <stack>  
  12. #include <ctype.h>  
  13. using namespace std;  
  14.   
  15. int puzzle [8][8];  
  16.   
  17. int get_h(){  
  18.     int a=0;  
  19.     int b=0;  
  20.     int c=0;  
  21.     for(int i=3;i<6;i++){  
  22.         for(int j=3;j<6;j++){  
  23.             if(puzzle[i][j]==1)a++;  
  24.             if(puzzle[i][j]==2)b++;  
  25.             if(puzzle[i][j]==3)c++;  
  26.         }  
  27.     }  
  28.     int ans=max(a,b);  
  29.     ans=max(ans,c);  
  30.     return 8-ans;  
  31. }  
  32.   
  33. void input(){  
  34.     scanf("%d",&puzzle[1][5]);  
  35.     scanf("%d",&puzzle[2][3]);  
  36.     scanf("%d",&puzzle[2][5]);  
  37.     for(int i=1;i<=7;i++){  
  38.         scanf("%d",&puzzle[3][i]);  
  39.     }  
  40.     scanf("%d",&puzzle[4][3]);  
  41.     scanf("%d",&puzzle[4][5]);  
  42.     for(int i=1;i<=7;i++){  
  43.         scanf("%d",&puzzle[5][i]);  
  44.     }  
  45.     scanf("%d",&puzzle[6][3]);  
  46.     scanf("%d",&puzzle[6][5]);  
  47.     scanf("%d",&puzzle[7][3]);  
  48.     scanf("%d",&puzzle[7][5]);  
  49. }  
  50.   
  51. void A(){  
  52.     int tmp=puzzle[1][3];  
  53.     for(int i=1;i<7;i++){  
  54.         puzzle[i][3]=puzzle[i+1][3];  
  55.     }  
  56.     puzzle[7][3]=tmp;  
  57. }  
  58. void B(){  
  59.     int tmp=puzzle[1][5];  
  60.     for(int i=1;i<7;i++){  
  61.         puzzle[i][5]=puzzle[i+1][5];  
  62.     }  
  63.     puzzle[7][5]=tmp;  
  64. }  
  65. void C(){  
  66.     int tmp=puzzle[3][7];  
  67.     for(int i=7;i>1;i--){  
  68.         puzzle[3][i]=puzzle[3][i-1];  
  69.     }  
  70.     puzzle[3][1]=tmp;  
  71. }  
  72. void D(){  
  73.     int tmp=puzzle[5][7];  
  74.     for(int i=7;i>1;i--){  
  75.         puzzle[5][i]=puzzle[5][i-1];  
  76.     }  
  77.     puzzle[5][1]=tmp;  
  78. }  
  79. void E(){  
  80.     int tmp=puzzle[7][5];  
  81.     for(int i=7;i>1;i--){  
  82.         puzzle[i][5]=puzzle[i-1][5];  
  83.     }  
  84.     puzzle[1][5]=tmp;  
  85. }  
  86. void F(){  
  87.     int tmp=puzzle[7][3];  
  88.     for(int i=7;i>1;i--){  
  89.         puzzle[i][3]=puzzle[i-1][3];  
  90.     }  
  91.     puzzle[1][3]=tmp;  
  92. }  
  93. void G(){  
  94.     int tmp=puzzle[5][1];  
  95.     for(int i=1;i<7;i++){  
  96.         puzzle[5][i]=puzzle[5][i+1];  
  97.     }  
  98.     puzzle[5][7]=tmp;  
  99. }  
  100. void H(){  
  101.     int tmp=puzzle[3][1];  
  102.     for(int i=1;i<7;i++){  
  103.         puzzle[3][i]=puzzle[3][i+1];  
  104.     }  
  105.     puzzle[3][7]=tmp;  
  106. }  
  107.   
  108. bool judge(){  
  109.     if( !(puzzle[3][3]-puzzle[3][4]) && !(puzzle[3][4]-puzzle[3][5]) && !(puzzle[3][4]-puzzle[3][5]) &&  
  110.     !(puzzle[3][5]-puzzle[4][5]) && !(puzzle[4][5]-puzzle[5][5]) && !(puzzle[5][5]-puzzle[5][4]) &&  
  111.     !(puzzle[5][4]-puzzle[5][3]) && !(puzzle[5][3]-puzzle[4][3]) ) {  
  112.         return true;  
  113.     }  
  114.     return false;  
  115. }  
  116.   
  117. char ans[100];  
  118. int depth=0;  
  119.   
  120. bool DFS(int d){  
  121.     if(d+get_h()>depth)return false;  
  122.     if(depth==d){  
  123.         if(judge()){  
  124.             return true;  
  125.         }else{  
  126.             return false;  
  127.         }  
  128.     }  
  129.   
  130.     A();  
  131.     if(DFS(d+1)){ans[d]='A';return true;}  
  132.     F();  
  133.       
  134.     B();  
  135.     if(DFS(d+1)){ans[d]='B';return true;}  
  136.     E();  
  137.       
  138.     C();  
  139.     if(DFS(d+1)){ans[d]='C';return true;}  
  140.     H();  
  141.       
  142.     D();  
  143.     if(DFS(d+1)){ans[d]='D';return true;}  
  144.     G();  
  145.       
  146.     E();  
  147.     if(DFS(d+1)){ans[d]='E';return true;}  
  148.     B();  
  149.       
  150.     F();  
  151.     if(DFS(d+1)){ans[d]='F';return true;}  
  152.     A();  
  153.       
  154.     G();  
  155.     if(DFS(d+1)){ans[d]='G';return true;}  
  156.     D();  
  157.       
  158.     H();  
  159.     if(DFS(d+1)){ans[d]='H';return true;}  
  160.     C();  
  161.       
  162.     return false;  
  163. }  
  164.   
  165. int main(){  
  166.     memset(puzzle,0,sizeof(puzzle));  
  167.     while(scanf("%d",&puzzle[1][3])){  
  168.         depth=0;  
  169.         memset(ans,0,sizeof(ans));  
  170.         if(!puzzle[1][3])break;  
  171.         input();  
  172.         while(!DFS(0)){  
  173.             depth++;  
  174.         }  
  175.         if(depth==0){  
  176.             printf("No moves needed\n");  
  177.         }else{  
  178.             printf("%s\n",ans);  
  179.         }  
  180.         printf("%d\n",puzzle[3][3]);  
  181.     }  
  182.     return 0;  
  183. }  
### 关于 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、付费专栏及课程。

余额充值