Program to find number of ways we can reach from top left point to bottom right point in Python

Suppose we have an N x M binary matrix where 0 represents an empty cell and 1 represents a blocked cell. Starting from the top-left corner, we need to find the number of ways to reach the bottom-right corner. We can only move right or down.

So, if the input matrix is ?

0 0 1
0 0 0
1 1 0

Then the output will be 2, as there are two valid paths: [Right, Down, Right, Down] and [Down, Right, Right, Down].

Algorithm

We use dynamic programming to solve this problem. The approach involves ?

  • Create a DP matrix of the same size as the input matrix, initialized with 0
  • Set dp[0][0] = 1 (one way to stay at the starting position)
  • Initialize the first row and first column based on blocked cells
  • Fill the DP matrix using the recurrence: dp[i][j] = dp[i-1][j] + dp[i][j-1]
  • Return dp[n-1][m-1] (bottom-right cell)

Implementation

class Solution:
    def solve(self, matrix):
        if not matrix or matrix[0][0] == 1:
            return 0
        
        rows, cols = len(matrix), len(matrix[0])
        dp = [[0] * cols for _ in range(rows)]
        
        # Starting point
        dp[0][0] = 1
        
        # Initialize first column
        for i in range(1, rows):
            if matrix[i][0] == 1:
                break
            dp[i][0] = 1
        
        # Initialize first row
        for j in range(1, cols):
            if matrix[0][j] == 1:
                break
            dp[0][j] = 1
        
        # Fill the DP table
        for i in range(1, rows):
            for j in range(1, cols):
                if matrix[i][j] == 1:
                    dp[i][j] = 0
                else:
                    dp[i][j] = dp[i-1][j] + dp[i][j-1]
        
        return dp[rows-1][cols-1]

# Test the solution
ob = Solution()
matrix = [
    [0, 0, 1],
    [0, 0, 0],
    [1, 1, 0]
]
print(ob.solve(matrix))
2

How It Works

The algorithm builds a DP table where dp[i][j] represents the number of ways to reach cell (i, j) from (0, 0). For each cell ?

  • If the cell is blocked (matrix[i][j] == 1), set dp[i][j] = 0
  • Otherwise, dp[i][j] = dp[i-1][j] + dp[i][j-1] (sum of ways from top and left)

Edge Cases

# Test edge cases
ob = Solution()

# Single cell - empty
print("Single empty cell:", ob.solve([[0]]))

# Single cell - blocked
print("Single blocked cell:", ob.solve([[1]]))

# Starting position blocked
print("Starting blocked:", ob.solve([[1, 0], [0, 0]]))

# No valid path
print("No path:", ob.solve([[0, 1], [1, 0]]))
Single empty cell: 1
Single blocked cell: 0
Starting blocked: 0
No path: 0

Time and Space Complexity

  • Time Complexity: O(N × M) where N and M are the dimensions of the matrix
  • Space Complexity: O(N × M) for the DP table

Conclusion

This dynamic programming solution efficiently counts all possible paths from top-left to bottom-right while avoiding blocked cells. The key insight is that the number of ways to reach any cell equals the sum of ways to reach its top and left neighbors.

Updated on: 2026-03-25T13:33:29+05:30

482 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements