Find the shortest distance for a rolling ball to stop at a maze destination

Quick Overview

Find the shortest distance a ball needs to stop exactly on a target cell in a grid maze, where every roll continues until the ball hits a wall or the edge. It tests graph modeling on grids, shortest paths with unequal edge weights, and careful simulation of the rolling rule.

Find the shortest distance for a rolling ball to stop at a maze destination

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A ball sits in a maze made of empty cells (`0`) and walls (`1`). The ball moves by rolling up, down, left or right, and once it starts rolling it keeps going until the next cell in that direction is a wall or lies outside the grid. When it stops, it may choose a new direction. Given the `m x n` grid `maze`, the ball's `start` position and a `destination`, return the shortest distance for the ball to stop at the destination. The distance is the number of empty cells the ball moves through, counting the destination and not counting the start cell. If the ball cannot stop at the destination, return `-1`. ### Function Signature ```python def shortest_distance(maze: list[list[int]], start: list[int], destination: list[int]) -> int: ``` ### Rules - `start` and `destination` are `[row, col]` pairs, 0-indexed. - The border of the grid acts as a wall: a ball rolling toward the edge stops on the last cell inside the grid. - Each one-cell move adds 1 to the distance. If a route crosses the same cell more than once, every crossing counts. - The ball reaches the destination only when it stops there. Rolling through the destination cell without stopping does not count. ### Constraints - `1 <= m, n <= 100` - `maze[i][j]` is `0` or `1` - `start.length == 2` and `destination.length == 2` - `0 <= start_row, destination_row < m` - `0 <= start_col, destination_col < n` - The start and the destination are both empty cells, and they are different cells. ### Examples ```text Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4] Output: 12 ``` One shortest route rolls left to (0,3), down to (1,3), left to (1,0), down to (2,0), right to (2,2), down to (4,2) and right to (4,4), covering 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12 cells. The only way into row 4 is down column 2 through the gap at (3,2), and no route stops at (2,2) for less than 8. ```text Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2] Output: -1 ``` The ball can roll through (3,2) moving up or down column 2, but the cells above and below it are empty, so it never stops there. Its left and right neighbors are walls, so it cannot arrive moving sideways either. ```text Input: maze = [[0,0,0],[0,1,0],[0,0,0]], start = [0,0], destination = [2,2] Output: 4 ``` Rolling right to (0,2) and then down to (2,2) covers 2 + 2 = 4 cells, and rolling down then right gives the same total. No route can be shorter than the 2 rows plus 2 columns that separate the two cells.

Overview: Find the shortest distance a ball needs to stop exactly on a target cell in a grid maze, where every roll continues until the ball hits a wall or the edge. It tests graph modeling on grids, shortest paths with unequal edge weights, and careful simulation of the rolling rule.

PracHub logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A ball sits in a maze made of empty cells (0) and walls (1). The ball moves by rolling up, down, left or right, and once it starts rolling it keeps going until the next cell in that direction is a wall or lies outside the grid. When it stops, it may choose a new direction.

Given the m x n grid maze, the ball's start position and a destination, return the shortest distance for the ball to stop at the destination. The distance is the number of empty cells the ball moves through, counting the destination and not counting the start cell. If the ball cannot stop at the destination, return -1.

Function Signature

def shortest_distance(maze: list[list[int]], start: list[int], destination: list[int]) -> int:

Rules

  • start and destination are [row, col] pairs, 0-indexed.
  • The border of the grid acts as a wall: a ball rolling toward the edge stops on the last cell inside the grid.
  • Each one-cell move adds 1 to the distance. If a route crosses the same cell more than once, every crossing counts.
  • The ball reaches the destination only when it stops there. Rolling through the destination cell without stopping does not count.

Constraints

  • 1 <= m, n <= 100
  • maze[i][j] is 0 or 1
  • start.length == 2 and destination.length == 2
  • 0 <= start_row, destination_row < m
  • 0 <= start_col, destination_col < n
  • The start and the destination are both empty cells, and they are different cells.

Examples

Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [4,4]
Output: 12

One shortest route rolls left to (0,3), down to (1,3), left to (1,0), down to (2,0), right to (2,2), down to (4,2) and right to (4,4), covering 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12 cells. The only way into row 4 is down column 2 through the gap at (3,2), and no route stops at (2,2) for less than 8.

Input: maze = [[0,0,1,0,0],[0,0,0,0,0],[0,0,0,1,0],[1,1,0,1,1],[0,0,0,0,0]], start = [0,4], destination = [3,2]
Output: -1

The ball can roll through (3,2) moving up or down column 2, but the cells above and below it are empty, so it never stops there. Its left and right neighbors are walls, so it cannot arrive moving sideways either.

Input: maze = [[0,0,0],[0,1,0],[0,0,0]], start = [0,0], destination = [2,2]
Output: 4

Rolling right to (0,2) and then down to (2,2) covers 2 + 2 = 4 cells, and rolling down then right gives the same total. No route can be shorter than the 2 rows plus 2 columns that separate the two cells.