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

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

Source

#include<cstdio>
#include<iostream>
using namespace std;
const int N=;
int xh[N][N-]={
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,,
,,,,,,
};
int mid[]={,,,,,,,};
int rev[]={,,,,,,,};
int flag,cnt[];
int path[(int)1e5+];
char op[]="ABCDEFGH";
int s[];
inline int h(){
for(int i=;i<=;i++) cnt[i]=;
for(int i=;i<;i++) cnt[s[mid[i]]]++;
return -max(max(cnt[],cnt[]),cnt[]);
}
inline void move(int k){
int t=s[xh[k][]];
for(int i=;i<;i++) s[xh[k][i-]]=s[xh[k][i]];
s[xh[k][]]=t;
}
inline int check(){
int v=s[mid[]];
for(int i=;i<;i++) if(s[mid[i]]!=v) return ;
return ;
}
void dfs(int depth,int lim){
int H=h();
if(depth+H>lim) return ;
if(check()){flag=;return ;}
for(int i=;i<;i++){
move(i);
path[depth]=i;
dfs(depth+,lim);
if(flag) return ;
move(rev[i]);
}
}
int main(){
while(~scanf("%d",&s[])&&s[]){
for(int i=;i<;i++) scanf("%d",&s[i]);
flag=;
for(int i=;;i++){
dfs(,i);
if(flag){
if(i){
for(int j=;j<i;j++) putchar(op[path[j]]);
putchar('\n');
}
else puts("No moves needed");
printf("%d\n",s[mid[]]);
break;
}
}
}
return ;
}

POJ2286 The Rotation Game[IDA*迭代加深搜索]的更多相关文章

  1. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  2. uva 11212 - Editing a Book(迭代加深搜索 IDA*) 迭代加深搜索

    迭代加深搜索 自己看的时候第一遍更本就看不懂..是非常水,但智商捉急也是没有办法的事情. 好在有几个同学已经是做过了这道题而且对迭代加深搜索的思路有了一定的了解,所以在某些不理解的地方询问了一下他们的 ...

  3. 埃及分数 迭代加深搜索 IDA*

    迭代加深搜索 IDA* 首先枚举当前选择的分数个数上限maxd,进行迭代加深 之后进行估价,假设当前分数之和为a,目标分数为b,当前考虑分数为1/c,那么如果1/c×(maxd - d)< a ...

  4. UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]

    解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...

  5. UVA 11212 Editing a Book [迭代加深搜索IDA*]

    11212 Editing a Book You have n equal-length paragraphs numbered 1 to n. Now you want to arrange the ...

  6. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  7. 7-10Editing aBook uva11212(迭代加深搜索 IDA*)

    题意:  给出n( 2<=n<=9) 个乱序的数组  要求拍成升序  每次 剪切一段加上粘贴一段算一次  拍成1 2 3 4 ...n即可     求排序次数 典型的状态空间搜索问题   ...

  8. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  9. UVA11212-Editing a Book(迭代加深搜索)

    Problem UVA11212-Editing a Book Accept:572  Submit:4428 Time Limit: 10000 mSec  Problem Description ...

随机推荐

  1. Oracle重置序列

    oracle序列创建以后,如果想重置序列从 0 开始,逐渐递增1,可以采用如下存储过程: create or replace procedure reset_seq( p_seq_name in va ...

  2. win7配置ftp服务器

    1.安装FTP,打开控制面板,找到程序和功能-->打开或者关闭windows功能,在列表中,展开internet信息服务,勾选FTP服务器,展开WEB管理工具,勾选IIS管理控制台,然后点击确定 ...

  3. KBEngine.executeRawDatabaseCommand使用

    先贴一段官方的API介绍: def executeRawDatabaseCommand( command, callback, threadID, dbInterfaceName ): 功能说明: 这 ...

  4. 聊聊Javascript中的AOP编程

    Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...

  5. 【F12】网络面板

    使用网络面板了解请求和下载的资源文件并优化网页加载性能 (1)网络面板基础 测量资源加载时间 使用 Network 面板测量您的网站网络性能. Network 面板记录页面上每个网络操作的相关信息,包 ...

  6. 好的API设计

    [非原创,原文链接] API设计书籍下载: 1.keynote.pdf 2.api-design.pdf 最近在重构公司的一个交互中间件,在重新设计API及总体架构的时候思考了许多, 不禁萌发了一个疑 ...

  7. php Laravel 框架之建立后台目录

    今天研究了在Laravel框架中的控制器中加入后台的目录.发现了一些小的规律,拿来和大家分享一下吧. 通常情况下,我们是直接在controllers目录中加入我们的控制器,然后再routes.php ...

  8. MetaSploit Pro 下载地址

    Windows: https://downloads.metasploit.com/data/releases/metasploit-latest-windows-installer.exe Linu ...

  9. linux下 安装mysql教程

    安装环境:系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...

  10. oracle最精简客户端(3个文件+1个path变量就搞定oracle客户端)

    oracle最精简客户端: network\admin\tnsnames.ora (自己新建)oci.dlloraocieill.dll 将oci.dll的路径加到path变量中就可以了 tnsnam ...