<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>

class Solution:
    
    def minPushBox(self, grid: List[List[str]]) -> int:

        def isValidCoordinate(x: int, y: int) -> bool:
            return 0 <= x < len(grid) and 0 <= y < len(grid[0]) and grid[x][y] != '#'

        def getAdjacentCoordinates(x: int, y: int) -> Iterable[tuple[int, int]]:
            for x2, y2 in [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]:
                if isValidCoordinate(x2, y2): 
                    yield (x2, y2)

        def isNextStateReachable(sx: int, sy: int, ex: int, ey: int, bx: int, by: int) -> bool:
            queue = deque([(sx, sy)])
            visited = {(sx, sy)}
            while queue:
                x, y = queue.popleft()
                if x == ex and y == ey: return True
                for x2, y2 in getAdjacentCoordinates(x, y):
                    if x2 == bx and y2 == by: continue
                    if (x2, y2) in visited: continue
                    queue.append((x2, y2))
                    visited.add((x2, y2))
            return False

        def getNextValidState(px: int, py: int, bx: int, by: int) -> Iterable[tuple[int, int, int, int]]:
            directions = [(-1,0),(0,1),(1,0),(0,-1)]
            for i, (xOffset, yOffset) in enumerate(directions):
                adjXOffset, adjYOffset = adjDirection = directions[(i + 2) % 4]
                bx2, by2 = bx + xOffset, by + yOffset
                ax, ay = bx + adjXOffset, by + adjYOffset
                if not isValidCoordinate(bx2, by2) or not isValidCoordinate(ax, ay): continue
                if not isNextStateReachable(px, py, ax, ay, bx, by): continue
                yield (bx, by, bx2, by2)

        def getInitialState() -> List[tuple[int, int]]:
            pCoordinates = bCoordinates = tCoordinates = (-1, -1)
            for i in range(len(grid)):
                for j in range(len(grid[0])):
                    if grid[i][j] == 'S':
                        pCoordinates = (i, j)
                    elif grid[i][j] == 'B':
                        bCoordinates = (i, j)
                    elif grid[i][j] == 'T':
                        tCoordinates = (i, j)
            return [pCoordinates, bCoordinates, tCoordinates]

        def breadthFirstSearch() -> int:
            (px, py), (bx, by), (tx, ty) = getInitialState()
            queue = deque([(px, py, bx, by)])
            visited = {(px, py, bx, by)}
            pushes = 0
            while queue:
                size = len(queue)
                for _ in range(size):
                    pxi, pyi, bxi, byi = queue.popleft()
                    if bxi == tx and byi == ty:
                        return pushes
                    for (pxf, pyf, bxf, byf) in getNextValidState(pxi, pyi, bxi, byi):
                        if (pxf, pyf, bxf, byf) in visited:
                            continue
                        queue.append((pxf, pyf, bxf, byf))
                        visited.add((pxf, pyf, bxf, byf))
                pushes += 1
            return -1

        return breadthFirstSearch()
178