[poj2286]The Rotation Game (IDA*)

//第一次在新博客里发文章好紧张怎么办

//MD巨神早已在一个小时前做完了

The Rotation Game

Time Limit: 15000MS Memory Limit: 150000K
Total Submissions: 5950 Accepted: 1992

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
------------------------------------------------------------------------------------------------------
poj英文题的尿性呵呵哒
大概就是把那个棋盘的abcdef轴旋转次数最少使得中间那几个数字相同(注意只有1,2,3)
那么先找规律,就是把输入数据的位置与abcdef对应
接下来因为搜索层数未知,dfs会TLE,bfs会MLE,所以容易想到ID搜索
先指定递归层数,然后迭代加深一层一层搜下去(有的说用二分但这种小数据反而更费时)
再看每一层的搜索方向
联想八数码问题(然而并不清楚八数码问题),发现每个状态都有最多递归的层数(八个位置贪心减去目前最多的数字个数)
那么写一个估价函数猜猜最多搜多少,如果超出了层数限制那就剪枝,这样就有了IDA*
(我在说些什么)
代码代码代码我要去睡

 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<limits.h> 5 int min(int a,int b){ 6     return a<b?a:b; 7 } 8 int lines[10][10]={ 9                    { 0, 2, 6,11,15,20,22},// A10                    { 1, 3, 8,12,17,21,23},// B11                    {10, 9, 8, 7, 6, 5, 4},// C12                      {19,18,17,16,15,14,13},// D13                    {23,21,17,12, 8, 3, 1},//E14                    {22,20,15,11, 6, 2, 0},//F15                    {13,14,15,16,17,18,19},//G16                    { 4, 5, 6, 7, 8, 9,10},//H17                   };18 int matrix[10]={6,7,8,11,12,15,16,17};19 int cross[30];20 char ans[100];21 int check(){22     for(int i=0;i<8;i++)if(cross[matrix[0]]!=cross[matrix[i]])return 0;23     return 1;24 }25 int predict(){26     int most=100;27     for(int i=1;i<=3;i++){28         int a=0;29         for(int j=0;j<8;j++)if(cross[matrix[j]]!=i)a++;30         most=min(most,a);31     }32     return most;33 }34 int rotate(int mode){35     int le=cross[lines[mode][0]];36     for(int i=0;i<6;i++)cross[lines[mode][i]]=cross[lines[mode][i+1]];37     cross[lines[mode][6]]=le;38     return 0;39 }40 int dfs(int dpt,int dptm){41     if(dpt==dptm) return check();42     if(dpt+predict()>dptm) return 0;43     for(int i=0;i<8;i++){44         ans[dpt]=i+‘A‘;45         rotate(i);46         if(dfs(dpt+1,dptm)) return 1;47         if(i%2==0)  rotate((i+5)%8);48         else  rotate((i+3)%8);49     }50     return 0;51 }52 int main(){53     while(scanf("%d",&cross[0])!=EOF&&cross[0]!=0){54           for(int i=1;i<24;i++) scanf("%d",&cross[i]);55           if(check()) printf("No moves needed\n");56           else{57                int i=0;58                while(++i) if(dfs(0,i)) break;59                ans[i]=‘\0‘;60                printf("%s\n",ans);61           }62           printf("%d\n",cross[matrix[0]]);63     }64     return 0;65 }
时间: 2024-08-15 15:50:01

[poj2286]The Rotation Game (IDA*)的相关文章

POJ2286 The Rotation Game

The Rotation Game Time Limit: 15000MS   Memory Limit: 150000KB   64bit IO Format: %I64d & %I64u 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,

hdu 1667 The Rotation Game ( IDA* )

题目大意: 给你一个"井"子状的board,对称的由24个方块组成,每个方块上有123三个数字中的一个.给你初始状态,共有八种变换方式,求字典序最小的最短的的变换路径使得,board中间的八个方块上数字相同.(建议看下题目中的图就懂啦). IDA*搜索. 我是干脆用结构体保存搜索状态(当然这样很占空间了,可能也耗时间,不过这题15s/150M的时空限制我也是醉了).保存一个board temp,一个搜索路径path,搜索深度n,以及一个内置的估值函数h().h()返回的是8减这八个方块

UVa1603 The Rotation Game (IDA*)

链接:http://acm.hust.edu.cn/vjudge/problem/36627分析:感觉这题前面的处理比较麻烦.首先把图中的位置从左到右从上到下编号,然后用一个二维数组记录8个方向上各个位置的编号,枚举最大深度maxd,乐观估计函数为至少需要移动次数,因为每次移动最多改变一个位置上的数字,所以中间8个位置除了出现次数最多的数字外的数字个数为x的话那么就至少要移动x次,枚举move8个方向再递归如果check为true则成功找到解返回,否则将a复位继续枚举,枚举完8个方向找不到解则失

UVa 1343 The Rotation Game(IDA*)

主要是设计乐观估计函数来减枝 假设中心区域有6个2,2个3,那肯定是消掉3最好,毕竟就两个. 那么理想情况下,旋转一次就能把一个3变成2,那么最少操作2次. 我们用h()来计算最少还要操作几次,其原理是假设中心区域都放1或2或3,返回至少操作的次数中最小的数 maxd是假设最多能操作的数; d是已经操作的数; 那么就可以得出乐观估计函数   h()+d>maxd 其含义为 : 若  至少还要操作次数  加上  已经操作的次数  大于  最多总共操作的次数就退出 . 其次就是节点的处理了,编个号数

POJ - 2286 - The Rotation Game (IDA*)

IDA*算法,即迭代加深的A*算法,实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #include

POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*

感觉这种算法还是比较局限的吧,重复搜索是一个不好的地方,而且需要高效的估值函数来进行强剪枝,这点比较困难. 迭代搜索深度是一个比较炫酷的搜索方式,不过有点拿时间换空间的感觉. 首先迭代深度比较搓的写法是,首先设置一个阀值MaxH,初始为最小值. 当在搜索深度Depth <= MaxH时找到解则此时为最优解,否则MaxH++,继续深搜. 另外一种比较吊的写法是二分搜索深度,若搜到则减小阀值,否则增大阀值. 总之,迭代深度搜索就是通过改变深搜的深度来寻找最优解,这样做的好处是省掉了BFS中状态标记所

The Rotation Game (poj 2286 搜索IDA*)

Language: Default The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5573   Accepted: 1878 Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked

UVa 1343 The Rotation Game (状态空间搜索 &amp;&amp; IDA*)

题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使中心8个格子数字一致的最少步骤,要输出具体的操作步骤及最终中心区域的数字.如果有多个解,输出字典序最小的操作步骤. 分析 : 还是状态空间的搜索,对象就是一个数字序列,判断中心位置是否一样,可以看出如果使用BFS,每一层还是爆炸,所以使用IDA*,关键还是模拟操作和h函数,这里的h函数是这样定义的,

POJ 2286 The Rotation Game(IDA*)

The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 6396   Accepted: 2153 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