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. 【高可用HA】Apache (3) —— Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy

    Mac下配置Apache Httpd负载均衡(Load Balancer)之mod_proxy httpd版本: httpd-2.4.17 参考来源: Apache (1) -- Mac下安装Apac ...

  2. Win10技巧:如何确定电脑是否适用Hyper-V虚拟机?

    既然微软想要为Hyper-V的普及铺路,那么各种套路……配套措施当然也会一并跟上.比如想要看出电脑是否符合Hyper-V配置要求,有至少两种方式可以参考. 方法一:系统信息 这方法很简单,在Corta ...

  3. C++实现顺序计算输入表达式的值

    #include <iostream> #include <cstring> #include <cctype>//判断字符类型需要的头文件 using names ...

  4. C++/C语言的标准库函数与运算符的区别new/delete malloc/free

    malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存.下面来看他们的区别. 一.操作对象有所不同 malloc与free是C++ ...

  5. 关于Unity的入门游戏飞机大战的开发(下)

    开发思路: 1: 修改测试模式,去掉开始按钮方便开发,加入敌机的资源2: 创建敌机 添加刚体,碰撞器组件,添加帧动画播放组件;3: 创建敌机出现的队形;4: 根据队形随机 生成我们的敌机,调整敌机的速 ...

  6. cmd命令行编码设置

    cmd窗口情况下:windows下cmd默认的编码是GBK 想在windows下查看sqlite的utf-8中文需要先 执行chcp 65001把当前页换为utf-8编码 chcp 命令: chcp ...

  7. QTableWidget的表头颜色设置

    设置水平和垂直表头的颜色ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{b ...

  8. vncserver的安装和使用

    环境:RedHat Linux 6企业版.Xwindows:gnome (红帽默认安装的图形界面) 尽管我们可以使用SSH连接远程通过字符界面来操作Linux,但是对于更多熟悉图形人来说是很不方便的, ...

  9. Tomcat 配置 项目 到tomcat目录外面 和 域名绑定访问(api接口、前端网站、后台管理网站)

    先停止tomcat服务 1.进入apache-tomcat-7.0.68/conf/Catalina/localhost(如果之前还都没有启动过tomcat,是不会有此目录的,先启动一次再关闭,会自动 ...

  10. 第二章 入门(MyBatis)

    本章将会以简略的步骤告诉你如何安装和创建 MyBatis-Spring,并构建一个简单的数据访问事务性的应用程序. Installation 要使用 MyBatis-Spring 模块,你只需要包含 ...