[kuangbin]专题二 搜索进阶 Eight II HDU - 3567【反向BFS】【康托展开】【哈希】

【题目描述】
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.

八数码,也被称为“九宫格”,来自一个古老的游戏。
在这个游戏中,你会得到一个3乘3的棋盘和8块瓷砖。瓷砖编号从1到8,每个覆盖一个网格。正如您所看到的,有一个空白的网格,可以表示为‘X’。与空白网格具有共同边缘的网格中的块可以移动到该空白网格中。这个操作导致用一个瓷砖交换“X”。
我们用符号‘r’来表示‘X’和右边的瓷砖交换,‘l’代表左边,‘u’代表上面的,‘d’代表下面的。
在这里插入图片描述
九宫格状态可以使用字符串S表示用如下展示的规则。
在这里插入图片描述
问题是操作一个‘r’,‘u’,‘l’,‘d’的操作列表,将九宫格的状态从状态A转到B状态。你需要找到满足以下约束的结果:
1.在所有可能的解决方案中,它的长度最小。
2.在所有最小长度的解中,它是字典序最小的解。

【输入】
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.
第一行是T(T<=200),表示这个问题的测试样例数。
每个测试用例的输入由两行组成,状态A占据第一行,状态B位于第二行。保证从A状态到B状态有可用的解决方案。

【输出】
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.
对于每个测试样例两行。
第一行“case x:d”的格式,其中x是从1计数的大小写数,d是您需要将A转换为B的最小操作列表长度
S是满足约束的操作列表,显示在第二行上。

【样例输入】
2
12X453786
12345678X
564178X23
7568X4123

【样例输出】
Case 1: 2
dd
Case 2: 8
urrulldr

题目链接:https://cn.vjudge.net/problem/HDU-3567

前一题已经自闭两天了,没想到还有一道hard版本
偷看大佬题解看到映射成九种情况第一反应是竟然还有这种操作,emmm……是我太菜了
其他的和前一题差不多的思路
偷看的题解在这里 → https://blog.csdn.net/codeswarrior/article/details/80313587
分成9种情况映射,bfs存下所有可能,vis标记方向,pre还原路径

代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
static const int MAXN=362880;
static const int fac[]={
   1,1,2,6,24,120,720,5040,40320};
static const int dx[]={
   1,0,0,-1},dy[]={
   0,-1,1,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值