#P2499. [SDOI2012] 象棋
[SDOI2012] 象棋
Description
Xiaoyun and Xiaonan, two sisters, have liked playing Chinese Chess since childhood. Now, as masters of the game, they are no longer interested in ordinary Chinese Chess, so they begin to play various new games using the board and pieces.
Today the weather is clear and sunny, and they are going to play on an board.
There are pieces and some blocked cells on the board. Let the top-left cell be and the bottom-right cell be . Parameters define the moves of all pieces: a piece at can move in one step to one of the eight cells $(x + a, y + b), (x + a, y - b), (x - a, y + b), (x - a, y - b), (x + b, y + a), (x + b, y - a), (x - b, y + a), (x - b, y - a)$. A piece can never jump out of the board or onto a blocked cell.
These pieces are identical. The goal is to move all pieces to specified cells in the minimum number of steps. During the moves, it is forbidden for multiple pieces to be on the same cell at the same time.
They have already found a plan with relatively few steps, but they are not sure whether it is optimal, so they ask you, as a programmer, for help.
Input Format
The first line contains five integers separated by spaces.
The next lines each contain a string of length describing the board, where . denotes a free cell and * denotes a blocked cell.
The next lines each contain two integers and , giving the initial positions of the pieces.
The next lines each contain two integers and , giving the target positions of the pieces.
Output Format
Output a single integer: the minimum number of steps needed to move all pieces to their target positions. The testdata guarantees that a solution exists.
1 8 2 2 0
.......*
1 1
1 3
1 5
1 7
4
Hint
[Sample explanation]
One feasible plan is as follows: the second piece jumps two steps to the right, then the first piece jumps two steps to the right, for a total of steps. Note that although the plan where the first piece jumps three steps to the right and then the second piece jumps one step to the right can also move all pieces to the target positions, the two pieces would simultaneously be at at some moment, which violates the rules, so this plan cannot be used.
Constraints
- For of the testdata, .
- Additionally, for of the testdata, .
- For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号