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. laravel中日志为daily时如何设置最大保存天数

    在laravel中,日志设置为daily时,默认保存七天的日志,超过则清除七天前的日志.可修改默认的设置,假如要保存30天的日志,则配置如下: 在配置文件config/app.php中添加如下代码: ...

  2. 记录第一次搭建svn服务器

    搭建svn服务器需要另外的软件, 在此以32位的为例: 安装过程非常简单, 一直下一步下一步确定就好了, svn安装完毕再安装中文语言包, 安装完成后可以在设置里面找到中文简体选择就OK了 主要记录一 ...

  3. lua闭合函数

    function count( ... ) return function( ... ) i = i+ return i end end local func = count(...) print(f ...

  4. mb_系列函数和普通字符函数的区别

    <?php //phpinfo(); $str = 'abcdef'; echo strlen($str);// 6 echo '<br/>'; echo substr($str, ...

  5. 如何用Maven创建web项目(具体步骤)

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Creat ...

  6. 【python】将一个正整数分解质因数

    def reduceNum(n): '''题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5''' print '{} = '.format(n), : print 'Pleas ...

  7. IOS常见错误之一连线错误

    在IOS编程中,UI方面,对于新手,接触时,不免喜欢拖控件,觉得省去了一些麻烦,其实在操作控件的过程中也有很多问题需要注意 本人今天就说下遇到的一个问题. setValue:forUndefinedK ...

  8. Bootstrap组件

    1.Bootstrap组件——Glyphicons图标字体 图标字体:可以表示的文字不是abcd或1234,而是一个又一个图形符号,比直接使用图片好处:可以任意放大不会失真:所有能使用文字的地方都可以 ...

  9. C#结构

    namespace STRUCT_TEST{ class Program { struct A { public int X;//不能直接对其进行赋值 public int Y; public sta ...

  10. Rails中的content_tag与concat用法,可以连接任意html元素

    想输出如下html <% if user.present? %> <li> <a href="<%= user_info_url(user.id) %&g ...