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. greendao操作数据库的使用方法

    第一步:把greendao-1.3.0-beta-1,greendao-generator-1.3.1两个jar包加载到工程的lib的文件夹中,一定要右键点击Add As Library后才能使用. ...

  2. sap MD04中常用函数

    1. 需求溯源 : MD_PEGGING_NODIALOG 2. 实时库存 : MD_STOCK_REQUIREMENTS_LIST_API 这个函数中MDPSX 和 MDEZX 是通过 MDPS 的 ...

  3. python迭代器和生成器

    迭代器 #可以被netxt()函数调用不断返回一个值的对象成为迭代器:Iterator #迭代器是访问集合元素的一种方式,从集合第一个元素开始(用next()方法)访问就不能回退,便于循环遍历一些较大 ...

  4. html5 图片上传,支持图片预览、压缩、及进度显示,兼容IE6+及标准浏览器

    以前写过上传组件,见 打造 html5 文件上传组件,实现进度显示及拖拽上传,兼容IE6+及其它标准浏览器,对付一般的上传没有问题,不过如果是上传图片,且需要预览的话,就力有不逮了,趁着闲暇时间,给上 ...

  5. 分享Db4o的便捷封装类源码

    导言 大家好,话说真是好久好久没写文章了,哈哈. 最近在写网站,个人对传统数据库天然抵触,感觉非常繁冗,即便是Entity Framework也过于庞杂了,Db4o这种轻量级且读写.配置都极其方便的新 ...

  6. 【笔记】Android项目添加项目引用方法

    刚才在做phoneGap时,想试图自己添加phoneGap的lib组件(jar的源码),找了好多种方法,下面这种成功了 项目邮件 Properties, Android ,Add...  ,然后Dep ...

  7. JAVA第4次作业

    package fuzhi; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExc ...

  8. Linux多线程服务端编程一些总结

    能接触这本书是因为上一个项目是用c++开发基于Linux的消息服务器,公司没有使用第三方的网络库,卷起袖子就开撸了.个人因为从业经验较短,主 要负责的是业务方面的编码.本着兴趣自己找了这本书.拿到书就 ...

  9. C/C++中extern关键字解析

    1 基本解释:extern可以置于变量或者函数前,以标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义.此外extern也可用来进行链接指定. 也就是说extern ...

  10. C# 并行编程 之 轻量级手动重置事件的使用

    目录(?)[-] 简单介绍 使用超时和取消 跨进程或AppDomain的同步   简单介绍 如果预计操作的等待的时间非常短,可以考虑使用轻量级的手动重置事件,ManualResetEventSlim. ...