<p>A storekeeper is a game in which the player pushes boxes around in a warehouse trying to get them to target locations.</p>

<p>The game is represented by an <code>m x n</code> grid of characters <code>grid</code> where each element is a wall, floor, or box.</p>

<p>Your task is to move the box <code>&#39;B&#39;</code> to the target position <code>&#39;T&#39;</code> under the following rules:</p>

<ul>
	<li>The character <code>&#39;S&#39;</code> represents the player. The player can move up, down, left, right in <code>grid</code> if it is a floor (empty cell).</li>
	<li>The character <code>&#39;.&#39;</code> represents the floor which means a free cell to walk.</li>
	<li>The character<font face="monospace">&nbsp;</font><code>&#39;#&#39;</code><font face="monospace">&nbsp;</font>represents the wall which means an obstacle (impossible to walk there).</li>
	<li>There is only one box <code>&#39;B&#39;</code> and one target cell <code>&#39;T&#39;</code> in the <code>grid</code>.</li>
	<li>The box can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. This is a <strong>push</strong>.</li>
	<li>The player cannot walk through the box.</li>
</ul>

<p>Return <em>the minimum number of <strong>pushes</strong> to move the box to the target</em>. If there is no way to reach the target, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/sample_1_1620.png" style="width: 500px; height: 335px;" />
<pre>
<strong>Input:</strong> grid = [[&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;T&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;,&quot;B&quot;,&quot;.&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;S&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> We return only the number of times the box is pushed.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;T&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;,&quot;B&quot;,&quot;.&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;.&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;S&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;]]
<strong>Output:</strong> -1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;T&quot;,&quot;.&quot;,&quot;.&quot;,&quot;#&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;#&quot;,&quot;B&quot;,&quot;.&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;S&quot;,&quot;#&quot;],
               [&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;,&quot;#&quot;]]
<strong>Output:</strong> 5
<strong>Explanation:</strong> push the box down, left, left, up and up.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 20</code></li>
	<li><code>grid</code> contains only characters <code>&#39;.&#39;</code>, <code>&#39;#&#39;</code>, <code>&#39;S&#39;</code>, <code>&#39;T&#39;</code>, or <code>&#39;B&#39;</code>.</li>
	<li>There is only one character <code>&#39;S&#39;</code>, <code>&#39;B&#39;</code>, and <code>&#39;T&#39;</code> in the <code>grid</code>.</li>
</ul>


from collections import deque

class Solution:
    def minPushBox(self, grid):
        from collections import deque

        m, n = len(grid), len(grid[0])

        # Directions: up, down, left, right
        dirs = [(-1,0),(1,0),(0,-1),(0,1)]

        # Find the positions of the box, player, and target
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 'S':
                    player_init = (i, j)
                if grid[i][j] == 'B':
                    box_init = (i, j)
                if grid[i][j] == 'T':
                    target = (i, j)

        # Check if a position is within bounds and not a wall
        def is_valid(x, y):
            return 0 <= x < m and 0 <= y < n and grid[x][y] != '#'

        # BFS to check if the player can reach (x, y) from (px, py) without passing through the box
        def can_reach(px, py, x, y, box_x, box_y):
            visited = [[False]*n for _ in range(m)]
            queue = deque()
            queue.append((px, py))
            visited[px][py] = True

            while queue:
                cur_x, cur_y = queue.popleft()
                if cur_x == x and cur_y == y:
                    return True
                for dx, dy in dirs:
                    nx, ny = cur_x + dx, cur_y + dy
                    if is_valid(nx, ny) and not visited[nx][ny]:
                        if (nx, ny) != (box_x, box_y):
                            visited[nx][ny] = True
                            queue.append((nx, ny))
            return False

        from heapq import heappush, heappop

        # State: (number of pushes, box_x, box_y, player_x, player_y)
        queue = []
        heappush(queue, (0, box_init[0], box_init[1], player_init[0], player_init[1]))
        visited = set()
        visited.add((box_init[0], box_init[1], player_init[0], player_init[1]))

        while queue:
            pushes, box_x, box_y, player_x, player_y = heappop(queue)
            if (box_x, box_y) == target:
                return pushes

            for dx, dy in dirs:
                new_box_x, new_box_y = box_x + dx, box_y + dy
                player_pos_x, player_pos_y = box_x - dx, box_y - dy

                if is_valid(new_box_x, new_box_y) and is_valid(player_pos_x, player_pos_y):
                    # Check if the player can reach the position to push the box
                    if can_reach(player_x, player_y, player_pos_x, player_pos_y, box_x, box_y):
                        state = (new_box_x, new_box_y, box_x, box_y)
                        if (new_box_x, new_box_y, box_x, box_y) not in visited:
                            visited.add((new_box_x, new_box_y, box_x, box_y))
                            heappush(queue, (pushes + 1, new_box_x, new_box_y, box_x, box_y))
        return -1
139