Ninja is playing a game where the player has to move a box to the target location. The game is represented by a ‘M x N’ grid ‘GRID’ of characters (‘P’, ‘T’, ‘B’, ‘|’, ‘.’), where:
The character ‘P’ represents the player. The player can move only in four directions, up, down, left, and right only if the is a floor.
The character ‘T’ represents the target location.
The character ‘B’ represents the box. There is only one box in the grid. The player can not move through the box.
The character ‘.’ represents the floor which means a free cell to walk.
The character ‘|’ represents the wall which means an obstacle.
The box can be pushed to an adjacent free cell by standing next to the box and then moving in the direction of the box.
Your task is to return the minimum number of pushes to move the box to the target. If there is no way to reach the target location, return -1.
For example: You are given grid = [["|","|","|","|","|","|"],
["|","T",".","B",".","|"],
["|",".","|","|",".","|"],
["|",".",".",".","S","|"],
["|","|","|","|","|","|"]]
The answer will be 2. The player has to push the box two times to move it to the target location.

The first line contains an integer 'C' which denotes the number of test cases.
The first line of each test case contains two space-separated integers, ‘M’ and ‘N’, representing the number of rows and columns, respectively.
The next ‘M’ line contains ‘N’ space-separated integers representing the ‘GRID’.
Output Format:
For each test case, print a single integer, denoting the minimum number of pushes required to move the box to the target location.
The output of each test case will be printed in a separate line.
1 <= C <= 10
1 <= M, N <= 20
GRID contains only characters ‘P’, ‘T’, ‘B’, ‘|’, or ‘.’.
There is only one character, ‘P’, ‘T’, and ‘B’ in the GRID.
Time limit: 1 sec
Note:
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
2
5 6
| | | | | |
| T . B . |
| . | | .|
| . . . P |
| | | | | |
3 3
| | |
T B P
| | |
2
1
For the first test case, the answer will be 2. The player has to push the box two times to move it to the target location.
For the second test case, the answer will be 1. The player has to push the box only once to move it to the target location.
2
1 4
T | B P
3 4
| | . |
T B . P
| | | |
-1
1
Use BFS to find the shortest path.
As we already know, the BFS can be used to find the shortest path from a source location to the target location. But that is not enough to solve this problem because we can move a box only if the player is in the opposite adjacent cell to the cell where we have to move the box. For example, if we have to move the box up the player should be below the box. So, to overcome this, we will initialize a queue that will store the state of the box and the player. Then for each state, we will check if the target location is reachable or not using another BFS.
Structure of graph created: Any vertice V denotes a game state and edge E denotes transition between states.
Algorithm:
O((M * N)^2), where M and N are numbers of rows and columns of the grid, respectively.
There can be a total of 4 * (M * N) game states in the worst case. For every state, we will check if it is possible to reach the target location which requires checking 4 * (M * N) states. Hence, the total time complexity will be O((M * N)^2).
O(M * N), where M and N are numbers of rows and columns of the grid, respectively.
The total space used by the queues in main minPushes() and isReachable() in the worst case is (M * N). Hence total space complexity is O(M * N).