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. yum报错:Error: xz compression not available

    测试服务器(centos6.5)经过一段时间的折腾,有一天在上面进行yum操作时突然出现下面的报错: Error: xz compression not available 最后经过一番排查,发现原因 ...

  2. 纯CSS3实现兔斯基简单害羞表情

    前言 很不巧前天突然就感冒了,都怪自己吃太多饼干导致上火了.整个人都无精打采.本来想多做几个兔斯基表情的,但身体发热很难受.所以就只完成一个简单点的表情耍一耍. 正文 先看一下这个简单到不能再简单的小 ...

  3. Nuget如何管理本地的包

    1.在nuget中创建一个本地的程序包源

  4. C++ List的用法(整理)

    转自http://blog.csdn.net/lskyne/article/details/10418823 Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和 ...

  5. C语言 百炼成钢11

    //题目31:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 //判断第二个字母. #define _CRT_SECURE_NO_WARNINGS #include<st ...

  6. java中从1970-1-1到当前时间之间的毫秒数转换为oracle date

    java中System.currentTimeMillis()取到的是从1970-01-01 00:00:00.000到当前时间的毫秒数,一个long类型的值. 现在oracle数据库中某表中存取的是 ...

  7. ZooKeeper学习第七期--ZooKeeper一致性原理

    一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过ZooKeeper对分布式系统进行Master选举,来解决分布式系统的单点故障,如图所示. 图 1.1 ZooK ...

  8. VS2010报错无法编译:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid

    win7 64位 专业版 + vs2010 从vc6.0下转过来的一个项目,突然遇到这个问题. 解决方案: 用C:\Windows\winsxs\x86_netfx-cvtres_for_vc_and ...

  9. MPMoviePlayerViewController的使用 (不直接将播放器放到主视图控制器,而是放到一个内部模态视图控制器中)

    其实MPMoviePlayerController如果不作为嵌入视频来播放(例如在新闻中嵌入一个视频),通常在播放时都是占满一个屏幕的,特别是在 iPhone.iTouch上.因此从iOS3.2以后苹 ...

  10. 安装.NET CORE

    需要安装两个包 https://github.com/dotnet/cli 1. .NET Core Installer 2. .NET Core SDK Installer