Under construction

Still being built. Things may change

Patrick's Parabox

Time limit4sMemory limit1024 MB

Summary
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 n×mn\times m grid. Denote (i,j)(i,j) as the cell in row ii and column jj. Rows are numbered 1,2,…,n1, 2, \ldots, n from top to bottom, and columns are numbered 1,2,…,m1, 2, \ldots, m from left to right.

Denote W, S, A, and D as the control commands, which move up, down, left, and right respectively.

Define v_W=(−1,0)v\_\text{W}=(-1,0), v_S=(1,0)v\_\text{S}=(1,0), v_A=(0,−1)v\_\text{A}=(0,-1), and v_D=(0,1)v\_\text{D}=(0,1).

Define w_W=(n,⌈m2⌉)w\_\text{W}=(n,\left\lceil\dfrac m2\right\rceil), w_S=(1,⌈m2⌉)w\_\text{S}=(1,\left\lceil\dfrac m2\right\rceil), w_A=(⌈n2⌉,m)w\_\text{A}=(\left\lceil \dfrac n2\right\rceil,m), and w_D=(⌈n2⌉,1)w\_\text{D}=(\left\lceil \dfrac n2\right\rceil,1).

In each operation, choose one control command cc from W, S, A, and D. Let pp be the cell containing the player and bb the cell containing the box before the operation.

  • If p+v_c=bp+v\_c=b and b+v_cb+v\_c is a floor cell, the player moves to p+v_cp+v\_c and the box moves to b+v_cb+v\_c. Only this case counts toward the answer, so it must happen as few times as possible.
  • If p+v_cp+v\_c is a wall cell, nothing happens.
  • If p+v_cp+v\_c is a floor cell and p+v_c≠bp+v\_c\ne b, the player moves to p+v_cp+v\_c.
  • If p+v_c=bp+v\_c=b and b+v_cb+v\_c is outside the grid, nothing happens.
  • If p+v_c=bp+v\_c=b, b+v_cb+v\_c is a wall cell, and w_cw\_c is a wall cell, nothing happens.
  • If p+v_c=bp+v\_c=b, b+v_cb+v\_c is a wall cell, and w_cw\_c is a floor cell, the player moves to w_cw\_c.
  • If p+v_cp+v\_c is outside the grid and b+v_cb+v\_c is a wall cell, nothing happens.
  • If p+v_cp+v\_c is outside the grid and b+v_cb+v\_c is outside the grid, nothing happens.
  • If p+v_cp+v\_c is outside the grid and b+v_cb+v\_c is a floor cell, the player moves to b+v_cb+v\_c.

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 TT (1≤T≤1051\le T\le 10^5), the number of test cases.

Each test case is given as follows.

The first line contains two integers nn and mm (2≤n,m≤1052 \le n, m \le 10^5), the size of the parabox.

Each of the next nn lines contains a string of length mm. 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 n⋅mn \cdot m over all test cases does not exceed 4⋅1054 \cdot 10^5.

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.

Examples2

  1. Example 1

    Input
    3
    9 9
    #########
    #####..-#
    #..=##.##
    #.p.##.##
    ....##.##
    #...b..##
    #...##.##
    #....####
    ####.####
    9 9
    #########
    #.......#
    #.#####.#
    #.#=....#
    ..#....-#
    ###.p.#.#
    #.....#b#
    #.....#.#
    ####.####
    9 9
    ####.####
    #....####
    #.####.##
    #.......#
    #.......#
    ###.#####
    #=.b#..##
    #-..p..##
    #########
    
    Expected output
    7
    4
    19
    
  2. Example 2

    Input
    1
    2 2
    pb
    -=
    
    Expected output
    -1