Puzzle: Patrick's Parabox
时间限制:C/C++/Rust/Pascal 4秒,其他语言8秒
空间限制:C/C++/Rust/Pascal 512 M,其他语言1024 M
64bit IO Format: %lld

题目描述

Patrick's Parabox is a Sokoban-like game. A Sokoban puzzle is a gird, and each cell is a wall or a floor. There are several boxes and a player in some distinct floor cells, and they can not move to the wall cells or coincide. You can control the player to move in one of four directions, left, right, up, and down. When the player touches a box, it can push the box. The target is to move all boxes to some target cells.

Please read the following rules carefully. They may be different from the usual rules

In this problem, there is only one box, and the box is the grid itself. That means if the player moves out of the grid, it may be ``teleported'' to a cell adjacent to the box; if the player moves to the box, it may be ``teleported'' to a cell on the boundary of the grid. Besides, there is also a target cell for the player. The player needs to move to the target cell at the end too.

Given a puzzle, you need to find the minimum times to push the box, such that the box and the player move to the target cell separately.

The following are the detailed and formal rules.

Consider an  grid. Denote (i,j) as the cell in i-th row and j-the column. The rows are numbered 1, 2, , n from top to bottom, and the columns are numbered 1, 2, , m from left to right.

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

Define that , , , .

Define that , , , .

In each operation, you can choose one of the control commands c, one of W, S, A, and D. Denote p as the cell which contains the player and b as the cell which contains the box before the operation:

  • If and is a floor cell, the player moves to and the box moves to . Only this case will be counted in the answer.
  • 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 w_c is a wall cell, nothing happens.
  • If , is a wall cell and w_c is a floor cell, the player moves to w_c.
  • 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 .

Note that the above are listed for covering all possibilities, but the operations are valid in only four of them.

输入描述:

There are multiple test cases. The first line of input contains an integer T(), the number of test cases.

For each test case:

The first line contains two integers n and m(, ), the size of the parabox.

Each of the following n lines contains a string of length m. Each character is one of ”#”, ”.”, ”p”, ”b”, ”=” and ”-”. ”#” denotes the wall cell, ”p” denotes the floor cell which contains the player, ”b” denotes the floor cell which contains the box, ”=” denotes the target floor cell of the player, ”-” denotes the target floor cell of the box, and ”.” denotes the other floor cell.

It is guaranteed that each of ”p”, ”b”, ”=”, ”-” occurs exactly once.

It is guaranteed that the sum of nm over all test cases does not exceed .

输出描述:

For each test case:

If it is impossible to move the box and the player to the target cells, output -1. Otherwise, output an integer -- the minimum times to push the box.
示例1

输入

复制
3
9 9
#########
#####..-#
#..=##.##
#.p.##.##
....##.##
#...b..##
#...##.##
#....####
####.####
9 9
#########
#.......#
#.#####.#
#.#=....#
..#....-#
###.p.#.#
#.....#b#
#.....#.#
####.####
9 9
####.####
#....####
#.####.##
#.......#
#.......#
###.#####
#=.b#..##
#-..p..##
#########

输出

复制
7
4
19

说明

The three puzzles in the first example are real levels in the game.
示例2

输入

复制
1
2 2
pb
-=

输出

复制
-1