Ugly solution
Code\n\nclass Solution:\n    def minPushBox(self, grid: List[List[str]]) -> int:\n        def canvisit(r1, c1, r2, c2, r3, c3):\n            if (r1, c1) == (r2,
# Code\n```\nclass Solution:\n    def minPushBox(self, grid: List[List[str]]) -> int:\n        def canvisit(r1, c1, r2, c2, r3, c3):\n            if (r1, c1) == (r2, c2): return True\n            grid[r3][c3] = \'R\'\n            q = [(r1, c1)]\n            seen = set([(r1, c1)])\n            while q:\n                r, c = q.pop()\n                for dr, dc in [[-1, 0], [1, 0], [0, 1], [0, -1]]:\n                    if 0<=r+dr<R and 0<=c+dc<C and grid[r+dr][c+dc] == \'.\' and (r+dr, c+dc) not in seen:\n                        if (r+dr, c+dc)==(r2, c2):\n                            grid[r3][c3] = \'.\'\n                            return True\n                        seen.add((r+dr, c+dc))\n                        q.append((r+dr, c+dc))\n            grid[r3][c3] = \'.\'\n            return False\n        @cache\n        def solve(r1, c1, r2, c2):\n            if (r2, c2) == goal: return 0\n            hsh = (r1, c1, r2, c2)\n            if hsh in memo: return memo[hsh]\n            memo[hsh] = res = 10**8\n            for dr, dc in [[1, 0], [0, 1]]:\n                if 0<=r2+dr<R and 0<=c2+dc<C and grid[r2+dr][c2+dc] == \'.\' and\\\n                    0<=r2-dr<R and 0<=c2-dc<C and grid[r2-dr][c2-dc] == \'.\':\n                    if canvisit(r1, c1, r2+dr, c2+dc, r2, c2):\n                        res = min(res, solve(r2, c2, r2-dr, c2-dc)+1)\n                    if canvisit(r1, c1, r2-dr, c2-dc, r2, c2):\n                        res = min(res, solve(r2, c2, r2+dr, c2+dc)+1)\n            grid[r2][c2] = \'.\'\n            memo[hsh] = res\n            return res\n        \n        memo = {}\n        R, C = len(grid), len(grid[0])\n        for i in range(R):\n            for j in range(C):\n                if grid[i][j] ==\'T\': \n                    goal = (i, j)\n                    grid[i][j] = \'.\'\n                if grid[i][j] == \'B\': \n                    r2, c2 = i, j\n                    grid[i][j] = \'.\'\n                if grid[i][j] == \'S\': \n                    r1, c1 = i, j\n                    grid[i][j] = \'.\'\n        return solve(r1, c1, r2, c2) if solve(r1, c1, r2, c2) != 10**8 else -1\n```
['Python3']