Push the Crate to the Mark
grid is a warehouse floor, one string per row: # is a wall, . is free floor, W is the worker, C is a crate and X is a mark on the floor.
The worker walks up, down, left or right over the floor, but not through walls or through the crate. Standing right next to the crate, the worker can push it one cell straight ahead, if that cell is inside the grid and not a wall. The worker then stands where the crate was.
Return the fewest pushes that bring the crate onto the mark, or -1 if that cannot be done. Walking costs nothing.
Example 1
The worker walks to the cell left of the crate and pushes it right twice.
Example 2
Push the crate up from below. Then walk around to its right side and push it left.
Example 3
The crate sits in a corner. To push it right, the worker would have to stand outside the grid.
Constraints
1 <= len(grid) <= 10and1 <= len(grid[0]) <= 10gridholds exactly oneW, oneCand oneX; every other cell is#or.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Roll Until It StopsHard
- Slide the Tiles Into PlaceHard
- Wall Off the OutbreakHard
- Water Held by the LandHard
- Column TotalsBeginner
- Count Negatives in a Sorted GridBeginner
- Cross TotalBeginner
- Enlarge the PictureBeginner