- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to find length of longest matrix path length in Python

Suppose we have a binary matrix, where 0 indicates empty cell and 1 indicates wall. We can start at any empty cell on first row and want to end up on any empty cell on the last row. We can move left, right, or down, we have to find the longest such path where we can visit each cell at most once. If this is not possible, then return 0.

So, if the input is like

0 | 0 | 0 | 0 |

0 | 0 | 0 | 1 |

0 | 0 | 0 | 0 |

then the output will be 10, as We can move (0, 3), (0, 2), (0, 1), (0, 0), (1, 0), (1, 1), (1, 2), (2, 2), (2, 1), (2, 0).

To solve this, we will follow these steps −

- N := row count of matrix
- M := column count of matrix
- dp := a list of size M and fill with -1
- for i in range 0 to N - 1, do
- ndp := a list of size M and fill with -1
- ndp2 := a list of size M and fill with -1
- for j in range 0 to M - 1, do
- if matrix[i, j] is not 1 and (i is same as 0 or dp[j] > -1) , then
- ndp[j] := dp[j] + 1
- ndp2[j] := dp[j] + 1

- if matrix[i, j] is not 1 and (i is same as 0 or dp[j] > -1) , then
- for j in range 1 to M - 1, do
- if matrix[i, j] is not 1 and ndp[j - 1] > -1, then
- ndp[j] := maximum of ndp[j] and (ndp[j - 1] + 1)

- if matrix[i, j] is not 1 and ndp[j - 1] > -1, then
- for j in range M - 2 to 0, decrease by 1, do
- if matrix[i, j] is not 1 and ndp2[j + 1] > -1, then
- ndp2[j] := maximum of ndp2[j] and (ndp2[j + 1] + 1)
- ndp[j] := maximum of ndp[j] and ndp2[j]

- if matrix[i, j] is not 1 and ndp2[j + 1] > -1, then
- dp := ndp

- return (maximum of dp) + 1

## Example

Let us see the following implementation to get better understanding −

def solve(matrix): N = len(matrix) M = len(matrix[0]) dp = [-1 for i in matrix[0]] for i in range(N): ndp = [-1 for j in matrix[0]] ndp2 = [-1 for j in matrix[0]] for j in range(M): if matrix[i][j] != 1 and (i == 0 or dp[j] > -1): ndp[j] = dp[j] + 1 ndp2[j] = dp[j] + 1 for j in range(1, M): if matrix[i][j] != 1 and ndp[j - 1] > -1: ndp[j] = max(ndp[j], ndp[j - 1] + 1) for j in range(M - 2, -1, -1): if matrix[i][j] != 1 and ndp2[j + 1] > -1: ndp2[j] = max(ndp2[j], ndp2[j + 1] + 1) ndp[j] = max(ndp[j], ndp2[j]) dp = ndp return max(dp) + 1 matrix = [ [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0] ] print(solve(matrix))

## Input

[ [0, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0] ]

## Output

10

- Related Articles
- Program to length of longest increasing path in a given matrix in Python
- Program to find length of longest path with even sum in Python
- Program to find length of longest alternating path of a binary tree in python
- Program to find length of longest consecutive path of a binary tree in python
- Program to find length of the longest path in an n-ary tree in Python
- Program to find length of the longest path in a DAG without repeated nodes in Python
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest consecutive sequence in Python
- Program to find length of longest distinct sublist in Python
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest palindromic substring in Python
- Program to find length of longest possible stick in Python?
- Program to find length of longest palindromic subsequence in Python
- Program to find length of longest fibonacci subsequence in Python

Advertisements