call(back)
FrontendmediumSign in to star problems

Unique Paths Grid Game

Build the grid game behind Unique Paths in React. A robot starts at the top-left cell and walks only right or down to its home at the bottom-right. The starter renders an empty grid; make it a game.

Requirements

  • Clicking a cell toggles an obstacle on it; the start and home cells cannot be blocked.
  • A status line shows how many distinct routes reach home, updated after every click, and says so when home is unreachable.
  • While at least one route exists, one of them is highlighted on the grid, moving only right and down over open cells.
  • "Reset" clears every obstacle.
  • Keep the grid immutable in state, and derive the count and the route from it rather than storing them.
  • Cells are buttons with aria-pressed for the obstacle state and a label naming the cell; the status line is a live region.

Follow-ups to be ready for: per-row and per-column costs with the cheapest route shown, animating the robot along the route, and a grid so large that the count overflows or the cells cannot all be rendered.

Hints

Hint 1

uniquePathsWithObstacles(grid) is one row of dynamic programming: a blocked cell contributes 0; every other cell adds the count from above (already in the row) and from the left.

Hint 2

For the route, mark from the far corner which cells can still reach home, then walk from the start choosing down whenever that stays on a reachable cell, otherwise right.

Hint 3

A reducer with TOGGLE and RESET keeps the grid immutable; useMemo on the grid gives the count and the route without a second piece of state.

Asked at

Similar Problems6