The Rotation Game

Time Limit: 15000MS

 

Memory Limit: 150000K

Total Submissions: 5691

 

Accepted: 1918

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

Shanghai 2004

【思路】

IDA*算法。

IDA*=ID-DFS+A*,即不断加深搜索的深度并设计一个乐观估计函数进行剪枝。

应用与本题中;

乐观估计:设中心8个数字最小不相同的数目为h,则至少需要dep+h个操作才能使得满足条件(dep为当前深度)。

迭代加深:不断增加操作数的数目,只要有符合的则中断迭代。

参考了紫书的附加代码,其中的hash有效简化了程序。

【代码】

 #include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<iostream>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; int hash[][]={
{,,,,,,},
{,,,,,,},
{,,,,,,},
{,,,,,,} };
const int rev[]={,,,,,,,};
const int certain[]={,,,,,,,}; int a[];
char ans[]; int diff(int c) {
int cnt=;
FOR(i,,) if(c!=a[certain[i]]) cnt++;
return cnt;
} int h() {
return min(min(diff(),diff()),diff());
} bool is_final() {
FOR(i,,) if(a[certain[]]!=a[certain[i]]) return false;
return true;
} void move(int i) {
int tmp=a[hash[i][]];
FOR(j,,) a[hash[i][j]]=a[hash[i][j+]];
a[hash[i][]]=tmp;
} bool IDDFS(int dep,int max_d) {
if(is_final()) {
ans[dep] = '\0';
cout<<ans<<"\n";
return true;
}
if(dep+h()>max_d) return false;
FOR(i,,) {
ans[dep] = 'A'+i;
move(i);
if(IDDFS(dep+,max_d)) return true;
move(rev[i]);
}
return false;
} int main()
{
FOR(i,,) {
FOR(j,,) hash[i][j]=hash[rev[i]][-j];
}
while(scanf("%d",&a[]) && a[])
{
FOR(i,,) scanf("%d",&a[i]);
FOR(i,,) if(!a[i]) return ;
if(is_final())
printf("No moves needed\n");
else {
for(int max_d=; ;max_d++)
if(IDDFS(,max_d)) break;
}
printf("%d\n",a[]);
}
return ;
}

POJ2286 The Rotation Game(IDA*)的更多相关文章

  1. POJ - 2286 - The Rotation Game (IDA*)

    IDA*算法,即迭代加深的A*算法.实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include < ...

  2. POJ 2286 The Rotation Game(IDA*)

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

  3. UVA-1343 The Rotation Game (IDA*)

    题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转 ...

  4. 【UVa】1343 The Rotation Game(IDA*)

    题目 题目     分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发.     代码 #include <cstdio> #include <algorit ...

  5. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  6. ArcGIS Desktop和Engine中对点要素图层Graduated Symbols渲染的实现 Rotation Symbol (转)

    摘要         ArcGIS中,对于要素图层的渲染,支持按照要素字段的值渲染要素的大小,其中Graduated Symbols可以对大小进行分级渲染.在个人开发系统的过程中,也可以用来美化数据显 ...

  7. UVA - 10384 The Wall Pusher(推门游戏)(IDA*)

    题意:从起点出发,可向东南西北4个方向走,如果前面没有墙则可走:如果前面只有一堵墙,则可将墙向前推一格,其余情况不可推动,且不能推动游戏区域边界上的墙.问走出迷宫的最少步数,输出任意一个移动序列. 分 ...

  8. 人类即将进入互联网梦境时代(IDA)

    在电影<盗梦空间>中,男主角科布和妻子在梦境中生活了50年,从楼宇.商铺.到河流浅滩.一草一木.这两位造梦师用意念建造了属于自己的梦境空间.你或许并不会想到,在不久未来,这看似科幻的情节将 ...

  9. The Rotation Game(IDA*算法)

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

随机推荐

  1. 关键词:CodeSmith工具、Money类型、__UNKNOWN__

    问题描述: 当数据库列类型有Money类型的时候,CodeSmith生成数据访问层会出错.有不能识别的类型.解决方法: 通过查找资料得知,数据库中的Money类型在DbType中是Currency(货 ...

  2. Socket 学习

    Socket一般应用模式(服务器端和客户端) 服务器端Socket(至少有两个) ->一个负责接收客户端连接请求(但不负责和客户端通信) ->没成功接收到一个客户端的连接便在服务端生成一个 ...

  3. 怎么捕获和记录SQL Server中发生的死锁

    我们知道,可以使用SQL Server自带的Profiler工具来跟踪死锁信息.但这种方式有一个很大的敝端,就是消耗很大.据国外某大神测试,profiler甚至可以占到服 务器总带宽的35%,所以,在 ...

  4. 转载[POJ题型分类]

    北大ACM题分类 主流算法: 1.搜索 //回溯 2.DP(动态规划) 3.贪心 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同等安置矩形的 ...

  5. System.Data.DbType的字符串和数据库中字符串类型对应关系

    前两天项目中因为历史原因数据库中的一个字段是varchar类型,在做SQL参数化处理时候默认都是DbType.String, 免得查询出现数据转换,于是做类型一致,搜了下对应关系还没找到,只好自己打开 ...

  6. svn的初级使用

    首先呢 你需要下载一个软件  比如说是 Cornerstone. 进行安装好之后 然后 然后输入账号密码 就可以了 然后去xcode去进行相关的配置 点击第二个进入 偏好设置 点击最下边的+ 点击第二 ...

  7. 十五、C# 使用查询表达式的LINQ

    使用查询表达式的LINQ   本章介绍了一种新的语法,查询表达式.   1.查询表达式概述 2.特点:投射  筛选  排序   Let  分组 3.作为方法调用   标准查询运算符所实现的查询在功能上 ...

  8. 对数据预处理的一点理解[ZZ]

    数据预处理没有统一的标准,只能说是根据不同类型的分析数据和业务需求,在对数据特性做了充分的理解之后,再选择相关的数据预处理技术,一般会用到多种预处理技术,而且对每种处理之后的效果做些分析对比,这里面经 ...

  9. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...

  10. mysql 连接问题----转载

    最近碰到一个mysql5数据库的问题.就是一个标准的servlet/tomcat网络应用,后台使用mysql数据库.问题是待机一晚上后,第二天早上第一次登录总是失败.察看日志发现如下错误: “com. ...