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

正解:迭代加深搜索(+大剪枝)

解题报告:

  北大信息学夏令营一试第一题,当时考场上看没什么人做于是果断没做,考完再看才发现是个搜索题。

  今天考试居然考到了这道题,好吧,就来填这个坑吧。

首先迭代加深,确定搜索深度上界(保证dfs不会gi),然后我们考虑各种奇奇怪怪的剪枝。

  显然,我们会在最终得到中间的一圈全是1或2或3,不妨取一个目前已经数量最多的数字来做一个估价若已经超过上界,就直接返回。

  还有一个剪枝,如果上一次往上移动,那么这一次就不要往下移动。

  代码如下:

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#ifdef WIN32
#define OT "%I64d"
#else
#define OT "%lld"
#endif
using namespace std;
typedef long long LL;
/*
图形一览:
0 1
2 3
4 5 6 7 8 9 10
11 12
13 14 15 16 17 18 19
20 21
22 23
*/
int num[][]={{,,,,,,},{,,,,,,},{,,,,,,},{,,,,,,}};
int ying[]={,,,,,,,};
int center[]={,,,,,,,};
int a[];
int ans,sh;
char out[]; inline int getint()
{
int w=,q=;
char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar();
if (c=='-') q=, c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar();
return q ? -w : w;
} inline bool check(){
int xun=a[center[]];
for(int i=;i<=;i++) if(a[center[i]]!=xun) return false;
return true;
} inline void mov(int x){
int cun=a[num[x][]];
for(int i=;i<=;i++) {
a[num[x][i]]=a[num[x][i+]];
}
a[num[x][]]=cun;
} inline int count(int x){
int total=;
for(int i=;i<=;i++) if(a[center[i]]!=x) total++;
return total;
} inline int gu(){
return min( min(count(),count()) , count());
} inline void init(){
for(int i=;i<=;i++) for(int j=;j<=;j++) num[i][j]=num[ying[i]][-j];
ying[]=;
} inline bool dfs(int tim,int limit,int last){
if(check()){
//out[tim]=a[center[0]];
sh=a[center[]];
return true;
}
else {
if(gu()+tim>limit) return false;
for(int i=;i<;i++) {
if(ying[last]==i) continue;
out[tim]='A'+i;
mov(i);
if(dfs(tim+,limit,i)) return true;
mov(ying[i]);
}
}
return false;
} inline void solve(){
init();
while(){
a[]=getint();
if(!a[]) break;
for(int i=;i<=;i++) a[i]=getint(); if(check()) printf("No moves needed\n%d\n",a[center[]]);
else{
for(ans=;;ans++) {
if(dfs(,ans,)) break;
}
printf("%s",out);
printf("\n%d\n",sh);
}
memset(out,'\0',sizeof(out));
}
} int main()
{
solve();
return ;
}

POJ2286 The Rotation Game的更多相关文章

  1. [poj2286]The Rotation Game (IDA*)

    //第一次在新博客里发文章好紧张怎么办 //MD巨神早已在一个小时前做完了 The Rotation Game Time Limit: 15000MS Memory Limit: 150000K To ...

  2. POJ2286 The Rotation Game(IDA*)

    The Rotation Game Time Limit: 15000MS   Memory Limit: 150000K Total Submissions: 5691   Accepted: 19 ...

  3. POJ2286 The Rotation Game[IDA*迭代加深搜索]

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

  4. A*专题训练

    POJ2449 Remmarguts' Date UDF's capital consists of N stations. The hall is numbered S, while the sta ...

  5. POJ2286:The Rotation Game——题解

    http://poj.org/problem?id=2286 题目大意:如图所示有一种玩具,每次可以拉动A-H的开关使得整个行(或列)向字母方向移动一位(如果移动到头的话则到行(列)尾部) 求使得中间 ...

  6. UVALive 7139 Rotation(矩阵前缀和)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  7. The Rotation Game(IDA*算法)

    The Rotation Game Time Limit : 30000/15000ms (Java/Other)   Memory Limit : 300000/150000K (Java/Othe ...

  8. unity3d 的Quaternion.identity和transform.rotation区别是什么

    Quaternion.identity就是指Quaternion(0,0,0,0),就是每旋转前的初始角度,是一个确切的值,而transform.rotation是指本物体的角度,值是不确定的,比如可 ...

  9. ios layer 动画-(transform.rotation篇)

    x轴旋转: CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"tra ...

随机推荐

  1. ref out 方法参数

    ref out 相似 ref和out两个关键字的作用大致相同,但是有一些微妙但是重要的区别. 两者的行为相似到连编译器都认为这两者不能被重载:public void SampleMethod(out ...

  2. SAP中主数据和单据的删除

    在SAP实际操作的过程中,有些主数据或者单据需要删除,但是删除的方法却不尽相同,所以笔者今天总结了下,供大家参考. 1,用T-Code去删除 例如我们要删除某个物料,我们可以用T-Code MM06 ...

  3. 使用DataAnnotations实现数据验证

    https://msdn.microsoft.com/zh-cn/library/system.componentmodel.dataannotations.aspx http://www.cnblo ...

  4. 千份位Javascript Thousand Separator / string format

    function Separator(str){ return str.split(/(\d+)(\d{3})(\d{3})(\d{3})(\d{3})/).join(',').replace(/^, ...

  5. 【Andorid------手势识别】GestureDetector和SimpleOnGestureListener的使用教程(转)——

    FROM:http://www.cnblogs.com/transmuse/archive/2010/12/02/1894833.html 1. 当用户触摸屏幕的时候,会产生许多手势,例如down,u ...

  6. Objective-c内存管理

    cocoa中的内存管理机制 引用计数 每一个对象都拥有一个引用计数 当对象创建的时候,引用计数的值是1 当发生retain消息时,该对象的引用计数+1,该对象的引用计数为2 当向这个对象发送relea ...

  7. AS2.0大步更新 Google强势逆天

    New Features in Android Studio 2.0Instant Run: Faster Build & Deploy逆天吗?你还在羡慕iOS的playground吗?And ...

  8. 从0开始学java——Tomcat的安装及其常见错误排查(zz)

    来源: <http://www.cnblogs.com/xdp-gacl/p/3729033.html> 学习web开发,为什么必须要先装一个WEB服务器? 在本地计算机上随便创建一个we ...

  9. 从0开始学java——JSP&Servlet——web容器搜索class的路径顺序

    在web应用程序如果要用到某个类,会按照如下的顺序来搜索: 1)在WEB-INF/classes目录下搜索: 2)如果该目录下没有,则会到WEB-INF/lib目录下的jar文件中搜索: 3)如果还没 ...

  10. 反射中getMethods 与 getDeclaredMethods 的区别

    public Method[] getMethods()返回某个类的所有公用(public)方法包括其继承类的公用方法,当然也包括它所实现接口的方法.public Method[] getDeclar ...