<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:
        
        '''
        1. whether s could move to the position to push b [possible to use a memo to save , does it change through time i.e., box position?]
        2. whehter b can be moved to the direction [just chekc whether it is '.' or 'T']
        '''
        def check(curr_people, next_people, curr_box):
            # optimization: use a memo to save visited
            if curr_people == next_people:
                return True
            # optimization: combine functions
            queue_people = deque([curr_people])
            visited_people = set()
            while queue_people:
                curr_x, curr_y = queue_people.popleft()
                for delta_x, delta_y in directions:
                    _x = curr_x + delta_x
                    _y = curr_y + delta_y
                    if 0 <= _x < n_row and 0 <= _y < n_col and grid[_x][_y] == '.' and (_x, _y) not in visited_people and (_x, _y) != curr_box:
                        if (_x, _y) == next_people:
                            return True
                        queue_people.append((_x, _y))
                        visited_people.add((_x, _y))
                
            return False
        
        n_row, n_col = len(grid), len(grid[0])
        for i in range(n_row):
            for j in range(n_col):
                if grid[i][j] == 'T':
                    t = (i, j)
                    grid[i][j] = '.'
                elif grid[i][j] == 'B':
                    b = (i, j)
                    grid[i][j] = '.'
                elif grid[i][j] == 'S':
                    s = (i, j)
                    grid[i][j] = '.'
                    
        queue = deque([(b, s, 0)])
        directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
        visited_box = set([(b, s)])
        
        while queue:
            for _ in range(len(queue)):
                curr_box, curr_people, curr_step = queue.popleft()
                if curr_box == t:
                    return curr_step
                people_candidates = []
                for delta_x, delta_y in directions:
                    _x = curr_box[0] + delta_x
                    _y = curr_box[1] + delta_y
                    if 0 <= _x < n_row and 0 <= _y < n_col and grid[_x][_y] == '.':
                        people_candidates.append((_x, _y))
                while people_candidates:
                    next_people = people_candidates.pop()
                    if check(curr_people, next_people, curr_box):
                        delta_x = next_people[0] - curr_box[0]
                        delta_y = next_people[1] - curr_box[1]
                        next_box = (curr_box[0] - delta_x, curr_box[1] - delta_y)
                        _x, _y = next_box
                        if 0 <= _x < n_row and 0 <= _y < n_col and grid[_x][_y] != '#' and (next_box, next_people) not in visited_box:
                            queue.append((next_box, next_people, curr_step + 1))
                            visited_box.add((next_box, next_people))
                        
                    
        return -1
    
    

        
            
        
130