<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 isValid(self, grid: List[List[str]], q: List[int] ):
        n = len(grid)
        m= len(grid[0])
        if q[0]>=0 and q[0]<n and q[1]>=0 and q[1]<m and grid[q[0]][q[1]]!="#":
            return True
        return False


    def connected(self, grid: List[List[str]], b: List[int], e: List[int], box: List[int]):
        if grid[e[0]][e[1]]=="#":
            return False
        if e[0]==b[0] and e[1]==b[1]:
            return True
        n = len(grid)
        m= len(grid[0])
        dist = [[-1 for j in range(m)] for i in range(n)]
        dist[b[0]][b[1]] = 0
        qu = [b]
        qu = deque(qu)
        direct = [[1,0], [-1,0], [0,1],[0,-1]]
        while len(qu)>0:
            q = qu.popleft()
            for d in direct:
                q0 = q[0]+d[0]
                q1 = q[1]+d[1]
                if self.isValid(grid, [q0,q1]) and (q0!=box[0] or q1!=box[1]) and dist[q0][q1]==-1:
                    dist[q0][q1]=dist[q[0]][q[1]]+1
                    qu.append([q0,q1])
                    if q0==e[0] and q1==e[1]:
                        return True

        return False
                                       
    def minPushBox(self, grid: List[List[str]]) -> int:
        n = len(grid)
        m= len(grid[0])
        #dist = [[-1 for j in range(m)] for i in range(n)]
        box = []
        b = []
        t=[]
        direct = [[1,0], [-1,0], [0,1],[0,-1]]
        for i in range(n):
            for j in range(m):
                if grid[i][j]=="B":
                    box=[i,j]
                if grid[i][j]=="S":
                    b=[i,j]
                if grid[i][j]=="T":
                    t=[i,j]
        qu = [[box[0], box[1], b[0], b[1], 0]]
        visited = set()
        qu=deque(qu)
        #dist[box[0]][box[1]]=0
        while len(qu) > 0:
            q = qu.popleft()
            box = [q[0], q[1]]
            b = [q[2], q[3]]
            dist = q[4]
            #print("pos")
            #print(b)
            #print("box")
            #print(box)
            for d in direct:
                if self.isValid(grid, [box[0]+d[0], box[1]+d[1]]) and self.isValid(grid, [box[0]-d[0], box[1]-d[1]])  and self.connected(grid, b, [box[0]-d[0], box[1]-d[1]], box) and (box[0], box[1], d[0], d[1]) not in visited:
                    #print("connection")
                    #print(b)
                    #print([box[0]-d[0], box[1]-d[1]])
                    qu.append([box[0]+d[0], box[1]+d[1], box[0], box[1], dist+1])
                    visited.add((box[0], box[1], d[0], d[1]))
                    if box[0]+d[0]==t[0] and box[1]+d[1]==t[1]:
                        return dist+1
                    

        return -1

        
163