What if you could instantly know all possible ways to reach your goal without getting lost in endless counting?
Why Unique Paths in Grid DP in DSA C?
Imagine you are in a city with a grid of streets. You want to walk from the top-left corner to the bottom-right corner, but you can only move right or down. Counting all possible routes by hand is like trying to list every path on a huge map.
Manually counting paths is slow and confusing. As the grid grows, the number of paths grows very fast, making it easy to lose track or make mistakes. It's like trying to count every grain of sand on a beach.
Dynamic Programming breaks the problem into smaller parts. It remembers the number of ways to reach each cell, so you don't repeat counting. This makes finding the total paths fast and easy, even for big grids.
int countPaths(int row, int col) {
if (row == 0 || col == 0) return 1;
return countPaths(row - 1, col) + countPaths(row, col - 1);
}int uniquePaths(int rows, int cols) {
int dp[rows][cols];
for (int i = 0; i < rows; i++) dp[i][0] = 1;
for (int j = 0; j < cols; j++) dp[0][j] = 1;
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[rows-1][cols-1];
}This lets you quickly find how many ways to reach a destination in a grid, unlocking solutions to many path and movement problems.
Robots in a warehouse need to find all possible routes to deliver items without backtracking. Using this method helps plan efficient paths.
Manual counting is slow and error-prone for large grids.
Dynamic Programming stores intermediate results to avoid repeated work.
This method efficiently calculates total unique paths in a grid.
Practice
Solution
Step 1: Understand the problem goal
The problem asks for counting all possible paths from the start (top-left) to the end (bottom-right) moving only right or down.Step 2: Clarify movement constraints
Only moves allowed are right or down, no diagonal or backward moves.Final Answer:
The number of different ways to move from the top-left to the bottom-right corner of a grid using only right and down moves. -> Option DQuick Check:
Unique paths = count of right/down moves combinations [OK]
- Thinking diagonal moves are allowed
- Confusing path count with shortest path
- Counting obstacles instead of paths
Solution
Step 1: Understand array initialization in C
Arrays cannot be assigned directly after declaration; memset is used to set all bytes to zero.Step 2: Check each option
int dp[m][n] = {0}; tries to initialize with {0} which works only for static arrays, not variable length arrays. int dp[m][n]; memset(dp, 0, sizeof(dp)); uses memset correctly. int dp[m][n]; dp = 0; tries invalid assignment. int dp[m][n] = NULL; assigns NULL which is invalid for arrays.Final Answer:
int dp[m][n]; memset(dp, 0, sizeof(dp)); -> Option CQuick Check:
Use memset to zero initialize arrays in C [OK]
- Trying to assign 0 directly to array
- Using NULL for arrays
- Assuming {0} works for variable length arrays
int uniquePaths(int m, int n) {
int dp[m][n];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}
printf("%d", uniquePaths(3, 3));Solution
Step 1: Initialize first row and column
First row and column are set to 1 because only one way to reach cells in first row (all right moves) or first column (all down moves).Step 2: Fill dp table using sum of top and left cells
For each cell dp[i][j], value = dp[i-1][j] + dp[i][j-1]. For 3x3 grid, dp[2][2] = 6.Final Answer:
6 -> Option BQuick Check:
Unique paths in 3x3 grid = 6 [OK]
- Not initializing first row and column to 1
- Adding instead of summing dp[i-1][j] and dp[i][j-1]
- Returning wrong dp cell
int uniquePaths(int m, int n) {
int dp[m][n];
memset(dp, 0, sizeof(dp));
for (int i = 0; i <= m; i++) dp[i][0] = 1;
for (int j = 0; j <= n; j++) dp[0][j] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[m-1][n-1];
}Solution
Step 1: Check loop bounds for initialization
Loops use <= m and <= n which causes index to go out of bounds since valid indices are 0 to m-1 and 0 to n-1.Step 2: Identify correct loop bounds
Loops should use < m and < n to avoid accessing invalid indices.Final Answer:
The loops initializing dp go out of bounds causing runtime error. -> Option AQuick Check:
Array indices must be within 0 to size-1 [OK]
- Using <= instead of < in loops
- Ignoring array bounds causing crashes
- Incorrect initialization of dp array
0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0
Solution
Step 1: Mark blocked cells in dp as zero
If a cell is blocked (value 1), no paths can go through it, so dp[i][j] = 0.Step 2: Calculate dp for free cells
For free cells (value 0), dp[i][j] = dp[i-1][j] + dp[i][j-1], summing paths from top and left.Final Answer:
Set dp[i][j] = 0 if cell is blocked; else dp[i][j] = dp[i-1][j] + dp[i][j-1]. -> Option AQuick Check:
Blocked cells have zero paths [OK]
- Ignoring obstacles in path count
- Assigning 1 to blocked cells
- Using diagonal moves to avoid obstacles
Ready to prove it?
Pass the test and this pattern moves unknown
