Dynamic Programming21 sections · 916 units
Open in Course

Grid 1 - Base Cases

Starting point

The base case is the starting cell: dp[1][1]=1dp[1][1] = 1 (one way to be at the start). But if the start cell is a wall, dp[1][1]=0dp[1][1] = 0 and the answer is 0 (no valid paths exist).

For the first row, you can only come from the left. For the first column, you can only come from above. Base cases are where the recursion stops. They handle the simplest possible inputs directly, without making recursive calls. Every recursive function needs at least one base case, or it will recurse forever and crash.