Profile DP Grid Paths - Problem
You are given an m x n grid where some cells may contain obstacles. You start at the top-left corner (0, 0) and want to reach the bottom-right corner (m-1, n-1).
You can only move right or down at any point in time. An obstacle is represented by 1 and an empty cell is represented by 0.
Count the number of unique paths from the start to the destination. This problem extends to broken profiles where certain row configurations may be invalid due to obstacles creating disconnected regions.
Use Profile Dynamic Programming with bitmasks to efficiently track valid row states and transitions between adjacent rows.
Input & Output
Example 1 — Basic Grid with Obstacle
$
Input:
grid = [[0,0,0],[0,1,0],[0,0,0]]
›
Output:
2
💡 Note:
Two paths: (0,0)→(0,1)→(0,2)→(1,2)→(2,2) and (0,0)→(1,0)→(2,0)→(2,1)→(2,2). Cannot go through the obstacle at (1,1).
Example 2 — Path Blocked
$
Input:
grid = [[0,1],[1,0]]
›
Output:
0
💡 Note:
No valid path exists. Starting at (0,0), we cannot move right due to obstacle at (0,1) and cannot move down due to obstacle at (1,0).
Example 3 — No Obstacles
$
Input:
grid = [[0,0],[0,0]]
›
Output:
2
💡 Note:
Two paths: (0,0)→(0,1)→(1,1) and (0,0)→(1,0)→(1,1). Classic grid path counting without obstacles.
Constraints
- 1 ≤ m, n ≤ 100
- grid[i][j] is 0 or 1
- grid[0][0] = grid[m-1][n-1] = 0
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code