Skip to content

Practice Minimum Moves to Move a Box to Their Target Location

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1263
LeetCode Problem

Minimum Moves to Move a Box to Their Target Location

Problem Statement

In grid, S is the player, B the single box, T the single target, . an empty floor cell, and # a wall. The player may walk up, down, left, or right through free cells, but not through walls or the box. To push the box one cell, the player must stand next to it on the opposite side and move toward it; the destination beyond the box must be free. Return the minimum number of pushes needed to put B on T, or -1 if impossible. Walking without pushing does not add to the push count.

Source: Minimum Moves to Move a Box to Their Target Location on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [["#","#","#","#","#","#"], ["#","T","#","#","#","#"], ["#",".",".","B",".","#"], ["#",".","#","#",".","#"], ["#",".",".",".","S","#"], ["#","#","#","#","#","#"]]
Output
3

# Example 2

Input
grid = [["#","#","#","#","#","#"], ["#","T","#","#","#","#"], ["#",".",".","B",".","#"], ["#","#","#","#",".","#"], ["#",".",".",".","S","#"], ["#","#","#","#","#","#"]]
Output
-1

# Example 3

Input
grid = [["#","#","#","#","#","#"], ["#","T",".",".","#","#"], ["#",".","#","B",".","#"], ["#",".",".",".",".","#"], ["#",".",".",".","S","#"], ["#","#","#","#","#","#"]]
Output
5

Constraints

m == grid.length
n == grid[i].length
1 <= m, n <= 20
grid contains only characters '.', '#', 'S', 'T', or 'B'.
There is only one character 'S', 'B', and 'T' in the grid.

Related LeetCode Problems

Practice more problems similar to Minimum Moves to Move a Box to Their Target Location.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.