<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
from typing import List

class Solution:
    def minPushBox(self, grid: List[List[str]]) -> int:
        # Helper function to convert 2D grid coordinates to a single index.
        def convert_to_flat_index(row: int, col: int) -> int:
            return row * num_cols + col

        # Check if the position (i, j) is within the grid and not blocked.
        def position_is_valid(row: int, col: int) -> bool:
            return 0 <= row < num_rows and 0 <= col < num_cols and grid[row][col] != "#"

        # Find the starting positions of the Player (S) and the Box (B).
        for r, row in enumerate(grid):
            for c, cell in enumerate(row):
                if cell == "S":
                    player_row, player_col = r, c
                elif cell == "B":
                    box_row, box_col = r, c

        # Dimensions of the grid
        num_rows, num_cols = len(grid), len(grid[0])

        # Possible 4 directions (up, right, down, left) as delta coordinates.
        direction_deltas = (-1, 0, 1, 0, -1)

        # Initialize the queue with the starting position of the player and box, with a move distance of 0.
        queue = deque([(convert_to_flat_index(player_row, player_col), convert_to_flat_index(box_row, box_col), 0)])

        # Visited states: Visited states are represented by player and box positions.
        visited = [[False] * (num_rows * num_cols) for _ in range(num_rows * num_cols)]
        visited[convert_to_flat_index(player_row, player_col)][convert_to_flat_index(box_row, box_col)] = True

        while queue:
            current_player, current_box, moves = queue.popleft()
            box_r, box_c = current_box // num_cols, current_box % num_cols

            # If the current box position is the target, return the number of moves.
            if grid[box_r][box_c] == "T":
                return moves

            player_r, player_c = current_player // num_cols, current_player % num_cols

            # Iterate over all possible moves.
            for delta_row, delta_col in zip(direction_deltas, direction_deltas[1:]):
                new_player_r, new_player_c = player_r + delta_row, player_c + delta_col

                # Check if the next player position is valid.
                if not position_is_valid(new_player_r, new_player_c):
                    continue

                # The player moves the box if they are adjacent.
                if new_player_r == box_r and new_player_c == box_c:
                    new_box_r, new_box_c = box_r + delta_row, box_c + delta_col

                    # Check if the new box position is valid and has not been visited.
                    if not position_is_valid(new_box_r, new_box_c) or visited[convert_to_flat_index(new_player_r, new_player_c)][convert_to_flat_index(new_box_r, new_box_c)]:
                        continue

                    # Update visited and add the new state to the queue.
                    visited[convert_to_flat_index(new_player_r, new_player_c)][convert_to_flat_index(new_box_r, new_box_c)] = True
                    queue.append((convert_to_flat_index(new_player_r, new_player_c), convert_to_flat_index(new_box_r, new_box_c), moves + 1))

                # If the player is not pushing the box, we add the new state to the front of the queue to check it sooner.
                elif not visited[convert_to_flat_index(new_player_r, new_player_c)][convert_to_flat_index(box_r, box_c)]:
                    visited[convert_to_flat_index(new_player_r, new_player_c)][convert_to_flat_index(box_r, box_c)] = True
                    queue.appendleft((convert_to_flat_index(new_player_r, new_player_c), convert_to_flat_index(box_r, box_c), moves))

        # Return -1 if it's impossible to push the box to the target.
        return -1

299