传送门HDU3567
描述
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:
- It is of minimum length among all possible solutions.
- It is the lexicographically smallest one of all solutions of minimum length.
输入
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.
输出
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.
样例
Input
2
12X453786
12345678X
564178X23
7568X4123Output
Case 1: 2
dd
Case 2: 8
urrulldr
题解
- 题意:与上一题不一样的是,此题要求从给定输入串处理到给定输出串
- 将字符串采用映射的方式进行预处理,然后O(t)查询,t为答案长度。
- 将输入串的X置0,其他的数从1到8一次排列形成映射,则初始状态就只有10种,预处理这10中状态能到达的状态。
- 对于样例中的564178X23,我们将其映射为123456078,则7568X4123就相应得被映射为512603478。
Code
|
|