The Rotation Game

Time Limit : 30000/15000ms (Java/Other)   Memory Limit : 300000/150000K (Java/Other)
Total Submission(s) : 29   Accepted Submission(s) : 12
Problem 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
 
 //AC代码:
#include <iostream>using namespace std;
int Ms[][] ={
, , ,,,,, //A操作对应的那一列在Map中的下标
, , ,,,,, //B操作......下同
, , , , , , ,
,,,,,,,
,,,, , , ,
,,,, , , ,
,,,,,,,
, , , , , ,,
};
int R[] = {,,,,,,,}; //各操作的相反操作在Ms中的行号
int Mid[] = {,,,,,,,}; //中间8块在Map中的下标
int Map[],op[],Depth; inline int MAX(int a,int b){ return a > b ? a : b;}
int Value() //估计当前状态到目标状态的最少需要多少操作
{
int t[] = {,,};
for(int i=;i<;++i)
++t[Map[Mid[i]]-];
return -MAX(t[],MAX(t[],t[]));
}
void Move(int k) //进行(k+'A')操作
{
int t = Map[Ms[k][]];
for(int i=;i<;++i)
Map[Ms[k][i]] = Map[Ms[k][i+]];
Map[Ms[k][]] = t;
}
bool Dfs(int depth){
if(depth == Depth) return false;
for(int i=;i<;++i)
{
Move(i);
op[depth] = i;
int v = Value();
if(v == ) return true;
if(depth+v < Depth && Dfs(depth+)) return true;
Move(R[i]);
}
return false;
}
int main(){
while(scanf("%d",&Map[]),Map[]) {
for(int i=;i<;++i)
scanf("%d",&Map[i]);
Depth = Value();
if(Depth == )
printf("No moves needed\n%d\n",Map[]);
else
{
memset(op,,sizeof(op));
while(!Dfs()) ++Depth;
for(int i=;i<Depth;++i)
printf("%c",'A'+op[i]);
printf("\n%d\n",Map[]);
}
}
return ;
}

没事来膜拜大牛的代码。。。

The Rotation Game(IDA*算法)的更多相关文章

  1. HUD 1043 Eight 八数码问题 A*算法 1667 The Rotation Game IDA*算法

    先是这周是搜索的题,网站:http://acm.hdu.edu.cn/webcontest/contest_show.php?cid=6041 主要内容是BFS,A*,IDA*,还有一道K短路的,.. ...

  2. 【学时总结】 ◆学时·II◆ IDA*算法

    [学时·II] IDA*算法 ■基本策略■ 如果状态数量太多了,优先队列也难以承受:不妨再回头看DFS-- A*算法是BFS的升级,那么IDA*算法是对A*算法的再优化,同时也是对迭代加深搜索(IDF ...

  3. HDU3567 Eight II —— IDA*算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)  ...

  4. [poj2286]The Rotation Game (IDA*)

    //第一次在新博客里发文章好紧张怎么办 //MD巨神早已在一个小时前做完了 The Rotation Game Time Limit: 15000MS Memory Limit: 150000K To ...

  5. 7-12 The Rotation Game IDA*

    状态搜索题目  一开始打算用bfs  但是图给的不是矩形图  有点难以下手 参考了 lrj    将图上所有的点进行标号  直接一个一维数组就解决了图的问题  并且明确了每个点的标号  处理起来十分方 ...

  6. POJ2286 The Rotation Game[IDA*迭代加深搜索]

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 6325   Accepted: 21 ...

  7. 八数码(IDA*算法)

    八数码 IDA*就是迭代加深和A*估价的结合 在迭代加深的过程中,用估计函数剪枝优化 并以比较优秀的顺序进行扩展,保证最早搜到最优解 需要空间比较小,有时跑得比A*还要快 #include<io ...

  8. HDU1560 DNA sequence —— IDA*算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Oth ...

  9. hdu 1667 The Rotation Game ( IDA* )

    题目大意: 给你一个“井”子状的board,对称的由24个方块组成,每个方块上有123三个数字中的一个.给你初始状态,共有八种变换方式,求字典序最小的最短的的变换路径使得,board中间的八个方块上数 ...

随机推荐

  1. java中的数据类型

    通常情况下,为了方便物品的存储,我们会规定每个盒子可以存放的物品种类,就好比在"放臭袜子的盒子"里我们是不会放"面包"的!同理,变量的存储也讲究"分门 ...

  2. 转载:align

    1. 原理    int a;    int size = 8;        <----> 1000(bin)计算a以size为倍数的下界数:    就让这个数(要计算的这个数)表示成二 ...

  3. DailyRollingFileAppender的使用

    DailyRollingFileAppender是日志记录软件包Log4J中的一个Appender,它能够按一定的频度滚动日志记录文件. 如果您不熟悉Log4J,建议阅读一下 使用Log4j进行日志记 ...

  4. NC nc5.x笔记(编辑中)

    一.设置卡片界面下 金额字段负数为红色! /** * 设置卡片界面下 金额字段负数为红色! */ private void repaintBodyMoneyColor(){ if(!isListPan ...

  5. Slight difference between C++ and C

    In C++, results of assignment operation, prefix increment and prefix decrement are all lvalues, the ...

  6. 3dmax渲染插件,生成2.5d瓦片

    基于3dmax2013的2.5d渲染插件,demo版,需要的和感兴趣的可以试用,这是百度网盘地址:http://pan.baidu.com/s/1c0mYY7e 插件主要功能,按层级对3dmax场景进 ...

  7. 对象的比较与排序:IComparable和IComparer接口

    IComparable和ICompare 接口是.net framework 中比较对象的标准方式,这两个接口提供一个返回值类似(大于0 等于0 小于0)的比较方法,二者区别如下: . ICompar ...

  8. 手机safari图片上传竖变横处理

    在手机safari上传图片时,竖着的照片会变成横着的照片,以下程序片段利用图片exif信息把图片旋转回去,代码抄自php.net官网. http://php.net/manual/zh/functio ...

  9. js判断字符串中是否含有指定汉语

    核心代码: function haveChinese(str,c){ if(escape(str).indexOf(escape(c))!=-1){ retrun true; } return fal ...

  10. ie8 iframe去掉边框的属性

    <iframe src="" id="Iframe" height="200" frameborder="0" s ...