Patrick's Parabox
Time limit4sMemory limit1024 MB
Given a grid where the grid itself is the only box, find the minimum number of pushes to move the box to its target and the player to its target.
- Level
Hard8 of 10
- Topics
- Simulation, BFS, Shortest path
- Solved
- No attempts yet
Problem
Patrick's Parabox is a Sokoban-like game. A Sokoban puzzle is a grid, and each cell is a wall or a floor. Several boxes and a player occupy distinct floor cells. They cannot move into wall cells or occupy the same cell. The player moves in one of four directions: left, right, up, or down. When the player touches a box, the player can push it. The goal is to move every box onto its target cell.
Read the following rules carefully. They may differ from the usual rules.
In this problem, there is only one box, and the box is the grid itself. If the player moves out of the grid, the player may be "teleported" to a cell adjacent to the box. If the player moves into the box, the player may be "teleported" to a cell on the boundary of the grid. The player also has a target cell, and the player must reach it at the end.
Given a puzzle, find the minimum number of pushes needed for the box and the player to reach their respective target cells.
The following are the detailed and formal rules.
Consider an grid. Denote as the cell in row and column . Rows are numbered from top to bottom, and columns are numbered from left to right.
Denote W, S, A, and D as the control commands, which move up, down, left, and right respectively.
Define , , , and .
Define , , , and .
In each operation, choose one control command from W, S, A, and D. Let be the cell containing the player and the cell containing the box before the operation.
- If and is a floor cell, the player moves to and the box moves to . Only this case counts toward the answer, so it must happen as few times as possible.
- If is a wall cell, nothing happens.
- If is a floor cell and , the player moves to .
- If and is outside the grid, nothing happens.
- If , is a wall cell, and is a wall cell, nothing happens.
- If , is a wall cell, and is a floor cell, the player moves to .
- If is outside the grid and is a wall cell, nothing happens.
- If is outside the grid and is outside the grid, nothing happens.
- If is outside the grid and is a floor cell, the player moves to .
The list covers every possible case. Only four of those cases are valid moves, and all others leave the state unchanged.
Input
The first line contains an integer (), the number of test cases.
Each test case is given as follows.
The first line contains two integers and (), the size of the parabox.
Each of the next lines contains a string of length . Each character is one of "#", ".", "p", "b", "=", or "-". Here "#" is a wall cell, "p" is the floor cell with the player, "b" is the floor cell with the box, "=" is the player's target floor cell, "-" is the box's target floor cell, and "." is any other floor cell.
Each of "p", "b", "=", and "-" appears exactly once.
The sum of over all test cases does not exceed .
Output
For each test case, print -1 if the box and the player cannot reach their respective target cells. Otherwise, print the minimum number of pushes on one line.
Hint
The three puzzles in the first example are real levels from the game.