1263. Minimum Moves to Move a Box to Their Target Location
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int minPushBox(char[][] grid) {
        int m = grid.length, n = grid[0].length;
        int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int[] box = new int[2], target = new int[2], player = new int[2];
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 'B') box = new int[]{i, j};
                else if (grid[i][j] == 'T') target = new int[]{i, j};
                else if (grid[i][j] == 'S') player = new int[]{i, j};
            }
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{box[0], box[1], player[0], player[1], 0});
        boolean[][][][] visited = new boolean[m][n][m][n];
        visited[box[0]][box[1]][player[0]][player[1]] = true;
        while (!queue.isEmpty()) {
            int[] curr = queue.poll();
            int bx = curr[0], by = curr[1], px = curr[2], py = curr[3], pushes = curr[4];
            if (bx == target[0] && by == target[1]) return pushes;
            for (int[] dir : directions) {
                int nx = bx + dir[0], ny = by + dir[1];
                int pxNew = bx - dir[0], pyNew = by - dir[1];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && pxNew >= 0 && pxNew < m && pyNew >= 0 && pyNew < n &&
                        grid[nx][ny] != '#' && grid[pxNew][pyNew] != '#' &&
                        !visited[nx][ny][bx][by] && canReach(grid, px, py, pxNew, pyNew, bx, by)) {
                    visited[nx][ny][bx][by] = true;
                    queue.add(new int[]{nx, ny, bx, by, pushes + 1});
                }
            }
        }
        return -1;
    }

    private boolean canReach(char[][] grid, int sx, int sy, int tx, int ty, int bx, int by) {
        int m = grid.length, n = grid[0].length;
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{sx, sy});
        boolean[][] visited = new boolean[m][n];
        visited[sx][sy] = true;
        while (!queue.isEmpty()) {
            int[] curr = queue.poll();
            int x = curr[0], y = curr[1];
            if (x == tx && y == ty) return true;
            for (int[] dir : new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}) {
                int nx = x + dir[0], ny = y + dir[1];
                if (nx >= 0 && nx < m && ny >= 0 && ny < n && grid[nx][ny] != '#' && !visited[nx][ny] && (nx != bx || ny != by)) {
                    visited[nx][ny] = true;
                    queue.add(new int[]{nx, ny});
                }
            }
        }
        return false;
    }
}

```
['Java']