Eight II

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)
Total Submission(s): 3449    Accepted Submission(s): 753

Problem Description

Eight-puzzle, which is also called "Nine grids", comes from an old game.

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.

A state of the board can be represented by a string S using the rule showed below.

The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.

 

Input

The first line is T (T <= 200), which means the number of test cases of this problem.

The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.

 

Output

For each test case two lines are expected.

The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.

 

Sample Input

2
12X453786
12345678X
564178X23
7568X4123
 

Sample Output

Case 1: 2
dd
Case 2: 8
urrulldr
 

Author

zhymaoiing
 

Source

 
 //2017-09-23
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map> using namespace std; const int INF = 0x3f3f3f3f;
int a[][], target, postion[], b[], d[], sx, sy, deep;
bool ok;
map<int, bool> vis;
char dir[] = {'d', 'l', 'r', 'u'};
int dx[] = {, , , -};//分别对应上下左右四个方向
int dy[] = {, -, , };
int kase; int Astar()
{
int h = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
if(a[i][j]!=)
{
int num = a[i][j];
int nx = (postion[num]-)/;
int ny = (postion[num]-)%;
h += (abs(i-nx-)+abs(j-ny-));
}
return h;
} int toInt()//把矩阵转换为int型数字
{
int res = ;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
res = res*+a[i][j];
return res;
} void IDAstar(int x, int y, int pre, int step)
{
if(ok)return ;
int h = Astar();
if(!h && toInt()==target)//找到答案
{
printf("Case %d: %d\n", ++kase, step);
for(int i = ; i < step; i++)
printf("%c", dir[d[i]]);
printf("\n");
ok = ;
return ;
}
if(step+h>deep)return ;//现实+理想<现状,则返回,IDA*最重要的剪枝
int now = toInt();
//if(vis[now])return ;//如果状态已经搜过了,剪枝,避免重复搜索
//vis[now] = true;
for(int i = ; i < ; i++)
{
if(i+pre == )continue;
int nx = x+dx[i];
int ny = y+dy[i];
if(nx>=&&nx<=&&ny>=&&ny<=)
{
d[step] = i;
swap(a[x][y], a[nx][ny]);
IDAstar(nx, ny, i, step+);
swap(a[x][y], a[nx][ny]);
d[step] = ;
}
}
return;
} char str1[], str2[]; int main()
{
int T;
scanf("%d", &T);
char ch;
kase = ;
while(T--)
{
ok = false;
deep = ;
int cnt = ;
scanf("%s%s", str1, str2);
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; j++)
{
ch = str1[(i-)*+j-];
if(ch == 'X')
{
a[i][j] = ;
sx = i;
sy = j;
}else
a[i][j] = ch - '';
b[cnt++] = a[i][j];
}
}
target = ;
getchar();
for(int i = ; i <= ; i++){
target *= ;
ch = str2[i-];
if(ch == 'X')
target += ;
else{
target += ch-'';
postion[ch-''] = i;
}
}
while(!ok)
{
vis.clear();
IDAstar(sx, sy, INF, );
deep++;//一层一层增加搜索的深度
}
} return ;
}

HDU3567的更多相关文章

  1. hdu3567 八数码(搜索)--预处理

    题意:为你两个状态,求a到b 的最小路径,要求字典序最小. 思路: 最开始想的是目标状态是变化的,所以打表应该不行,然后直接上A*,但是TLE了- -(瞬间无语) 然后看了下别人的思路,预处理出9个状 ...

  2. hdu3567 八数码2(康托展开+多次bfs+预处理)

    Eight II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)Total S ...

  3. HDU3567 Eight II —— IDA*算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)  ...

  4. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  5. HDU - 3567

    https://cn.vjudge.net/problem/HDU-3567 #include <stdio.h>#include <math.h>#include <q ...

  6. HDU 3820 Golden Eggs (SAP | Dinic)

    Golden Eggs Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. 如何将他人的SOPC工程转换为自己可以使用的工程

    上篇文章的程序源码在:http://download.csdn.net/detail/noticeable/9921952 源码错误现象: 在下载源码文件解压后,打开系统工程,可以看到quartus ...

  2. Django Auth 专题

    Django的标准库存放在 django.contrib 包中.每个子包都是一个独立的附加功能包. 这些子包一般是互相独立的,不过有些django.contrib子包需要依赖其他子包,其中django ...

  3. windows 批处理语言学习

    程序员应该根植于心的一个理念是:重复的工作交给代码.windows上的批处理脚本就是这种理念的体现. 批处理bat能做的事很多,自动配置vs工程中的代码依赖环境,调用其它程序处理数据.自动编译代码等等 ...

  4. shell 文件操作

      序   在linux平台下开发,我们经常会接触到一些任务性质的工作,而处理方式多样化.现积累各个案例.   从远程服务器拉取文件到本地服务器   scp work@cp01-xx-dev.com: ...

  5. myeclipise生成javadoc

    1.点击项目,右键,选择export: 点击next: 点击next:VM options中输入-encoding UTF-8 -charset UTF-8

  6. JSP标准标签库:JSTL

    JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能. jstl出现的目的同el一样也是要代替jsp页面中的脚本代码. ...

  7. ElasticSearch权威指南学习(分布式搜索)

    查询阶段 在初始化查询阶段(query phase),查询被向索引中的每个分片副本(原本或副本)广播. 每个分片在本地执行搜索并且建立了匹配document的优先队列(priority queue). ...

  8. Dispatch Queue 内存结构

    Dispatch 源代码版本是libdispatch-84.5.5  会根据这个结构来分析dispatch_queue 对应的代码实现 参考 GCD源码分析3 -- dispatch_queue篇 ...

  9. JAVA实现调用微信js-sdk扫一扫

    喜欢的朋友可以关注下. 已经很久没有给大家分享一片技术文章了,今天抽了点时间来,给大家说一说如何调用微信提供的扫一扫接口. 前提: 需要申请一个公众号:申请公众号需要的资料我就不说了,去申请微信会提示 ...

  10. Hadoop生态系统之HDFS

    一.介绍 HDFS : 分布式文件系统(distributed filesystem),主从结构. 以流式数据访问模式来存储超大文件,运行于商用硬件集群上. 超大文件: 几百M,几百G,甚至几百TB大 ...