#P1979. [NOIP 2013 提高组] 华容道
[NOIP 2013 提高组] 华容道
Description
小 B recently got hooked on the Huarong Dao sliding puzzle, but it always takes him a long time to finish one game. So he thought of writing a program to solve it: given a board position, determine whether the puzzle is impossible to finish, and if it can be finished, what is the minimum time required.
The Huarong Dao that 小 B plays is slightly different from the classic one. The rules are:
- On an board there are cells. Exactly one cell is empty, and each of the remaining cells has one piece on it. Every piece is of size .
- Some pieces are fixed, while others are movable.
- Any piece on a cell adjacent to the empty cell (sharing a side) can move into the empty cell.
The goal is to move a specified movable piece from its start position to a target position.
Given a board, the game can be played times. The fixed cells on the board never change, but the initial position of the empty cell, the specified movable piece’s start position, and its target position may differ each time. In the -th play, the empty cell is at row , column , the specified movable piece starts at row , column , and its target is row , column .
Assume 小 B can perform one piece-move per second, and all other operations take negligible time. For each game, tell 小 B the minimum time required, or tell him that the game cannot be completed.
Input Format
The first line contains integers, separated by a single space, which are .
The next lines describe an board. Each line contains integers separated by single spaces. Each integer describes the state of a cell: means the piece on that cell is fixed, and means the piece on that cell is movable or the cell is empty.
The next lines each contain integers , separated by single spaces, giving the empty cell’s position, the specified piece’s start position, and its target position for each game.
Output Format
Output lines. Each line contains integer, the minimum time required for that game, or if the target cannot be achieved.
3 4 2
0 1 1 1
0 1 1 0
0 1 0 0
3 2 1 2 2 2
1 2 2 2 3 2
2
-1
Hint
Explanation of the sample I/O:
Cells with an X are fixed, the red cell is the target position, and circles denote pieces; the green circle is the target piece.
-
In the first game, the empty cell starts at (as shown), and the goal is to move the piece initially at (the green circle) to the target position (the red cell).
The moves are as follows:

-
In the second game, the empty cell starts at (as shown), and the goal is to move the piece initially at (the green circle) to the target position .

To move the target piece into the target cell, the empty cell must first enter the target cell. For the empty cell to enter the target cell, it must swap with the piece currently on . After that, the only piece that can swap with the empty cell is that same piece on the target cell, so the target piece can never reach its target. The game is impossible.
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号