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
Number of Ways to Reach Destination in Grid INPUT [1,1] [1,2] [1,3] [2,1] [2,2] [2,3] S D Grid: n=2, m=3 Steps: k=3 Source: [1,1] Dest: [2,3] Movement Rules: - Same row OR same col - No staying in place - Exactly k steps - Result mod 10^9+7 ALGORITHM STEPS 1 Define DP State dp[step][x][y] = ways to reach (x,y) in step moves 2 Initialize Base dp[0][src_x][src_y] = 1 All others = 0 3 Transition For each step 1 to k: Sum paths from same row/col (exclude self) 4 Return Answer dp[k][dest_x][dest_y] mod 10^9 + 7 Optimization: Row/Col sums rowSum[s][x] colSum[s][y] FINAL RESULT 4 Valid Paths Found: Path 1: [1,1]-->[1,3] -->[2,3]-->[2,3] X Path 1: [1,1]-->[2,1] -->[2,3]-->[1,3]-->[2,3] Path 2: [1,1]-->[1,2] -->[2,2]-->[2,3] Path 3: [1,1]-->[1,3] -->[1,2]-->[2,2]-->[2,3] Path 4: [1,1]-->[2,1] -->[2,2]-->[2,3] OUTPUT 4 [OK] 4 ways in k=3 steps Time: O(k*n*m*(n+m)) Key Insight: Use Dynamic Programming with state dp[step][x][y]. For each cell, sum contributions from all cells in the same row and column (excluding itself). Optimize with precomputed row/column sums to avoid recalculating. This reduces redundant computation and handles the modulo efficiently. TutorialsPoint - Number of Ways to Reach Destination in the Grid | Optimal DP Solution
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