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. submit 读取mb52数据

    方法一: data:list_tab type table of abaplist.   data:vlist(300) type c occurs 0 with header line. submi ...

  2. git 常见命令

    查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...

  3. .net类库里ListView的一个BUG

    今天在CSDN论坛里看一个帖子,说是在ListView中添加了条目后第一行内容不显示,为了还原他的问题我写了以下代码. private void LoadFiles(DirectoryInfo dir ...

  4. RancherOS Hyper-V 安装

    RancherOS Hyper-V 安装, Install to disk 打开 Hyper-V 管理界器, 新建虚拟机 输入名称和存储位置 选择一代 最低 1024M 配置网络 创建虚拟磁盘 设置启 ...

  5. solr多词匹配搜索问题及解决

    使用solr进行某较长词搜索时出现了一些问题,及解决方案. 1.问题:solr默认使用OR方式搜索,当搜索一个很长的次,比如“XX集团股份有限公司”,分词器分词后,使用OR方式匹配,会匹配到很多结果. ...

  6. ng-init,ng-controller,ng-model

    1.ng-init 用于初始化数据,跟在$scope插入数据一样,但是在配合repeat指令时候比较有用: <div ng-repeat="arrOuter in arr" ...

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

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

  8. [Tools] Vim插件管理

    我们在使用插件的时候,都不希望插件安装的很杂乱,它不是一个看不见的黑盒,也为了下次方便在其它地方安装. 由于要方便插件管理,于是有了 Vundle,以下做些介绍: 1. 一个插件管理器, 自己本身也是 ...

  9. arcgis操作笔记-根据属性提取某区域要素

    1. 提取

  10. HTML5之tabindex属性

    1 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title&g ...