Number of Ways to Reach Destination in the Grid - Problem

Imagine you're navigating a grid-based board game where you can only move in straight lines! You start at a source position and need to reach a destination in exactly k moves.

The Challenge: Given an n ร— m grid (1-indexed), a starting position source [x, y], and a destination dest [x, y], find the number of ways to reach the destination in exactly k steps.

Movement Rules:

  • You can move from cell [x1, y1] to [x2, y2] if either x1 == x2 (same row) or y1 == y2 (same column)
  • You cannot stay in the same cell (no self-loops)
  • Each move counts as exactly 1 step

Goal: Return the total number of distinct paths of length k from source to destination, modulo 109 + 7.

Input & Output

example_1.py โ€” Basic Grid Navigation
$ Input: n = 2, m = 3, k = 3, source = [1, 1], dest = [2, 3]
โ€บ Output: 4
๐Ÿ’ก Note: There are 4 different ways to reach destination [2,3] from source [1,1] in exactly 3 steps. Some example paths: [1,1] โ†’ [1,2] โ†’ [2,2] โ†’ [2,3], [1,1] โ†’ [1,3] โ†’ [2,3] โ†’ [2,1] โ†’ [2,3], etc.
example_2.py โ€” Same Position
$ Input: n = 3, m = 3, k = 2, source = [1, 1], dest = [1, 1]
โ€บ Output: 16
๐Ÿ’ก Note: To return to the same position in exactly 2 steps, we can move to any of the 4 adjacent cells (2 in same row + 2 in same column), then return. Each of the 4 first moves has 4 possible return moves, giving 4ร—4 = 16 total paths.
example_3.py โ€” Impossible Case
$ Input: n = 2, m = 2, k = 1, source = [1, 1], dest = [2, 2]
โ€บ Output: 0
๐Ÿ’ก Note: From [1,1], we can only move to [1,2] or [2,1] in one step (same row or column). We cannot reach [2,2] directly in 1 step since it requires changing both row and column.

Constraints

  • 1 โ‰ค n, m โ‰ค 1000
  • 0 โ‰ค k โ‰ค 1000
  • 1 โ‰ค source[0], dest[0] โ‰ค n
  • 1 โ‰ค source[1], dest[1] โ‰ค m
  • Grid coordinates are 1-indexed

Visualization

Tap to expand
Source (1,1)Dest (4,3)Example Paths (k=4 steps):Path 1: (1,1)โ†’(1,2)โ†’(2,2)โ†’(2,3)โ†’(4,3)Path 2: (1,1)โ†’(2,1)โ†’(2,3)โ†’(4,3)โ†’(4,3)Key Insights:โ€ข Rook moves: same row OR columnโ€ข Must use exactly k stepsโ€ข Count ALL possible pathsโ€ข Memoize to avoid recomputation
Understanding the Visualization
1
Valid Moves
From any position, rook can move to any cell in same row or column
2
Path Counting
Each sequence of k moves represents a unique path
3
Memoization
Cache results for (position, remaining_steps) to avoid recomputation
4
Final Count
Sum all valid k-step paths from source to destination
Key Takeaway
๐ŸŽฏ Key Insight: Transform the grid navigation into a state transition problem where each (position, steps_remaining) represents a unique subproblem that can be memoized for optimal efficiency.
Asked in
Google 45 Microsoft 32 Meta 28 Amazon 25
43.2K Views
Medium-High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen