#K42937. Strictly Increasing Path in a Matrix
Strictly Increasing Path in a Matrix
Strictly Increasing Path in a Matrix
You are given an M×N matrix of integers. Your task is to determine whether there exists a path from the top-left corner to the bottom-right corner of the matrix such that all the values along the path are in strictly increasing order. You can only move either to the right or down at any step.
A valid path is one that starts from the top-left cell (1, 1) and reaches the bottom-right cell (M, N) by moving only in rightward or downward directions. Moreover, if the value at the current cell is v, then you can move to the next cell only if its value is greater than v.
The mathematical condition for a valid move from cell (i, j) to cell (k, l) (where k = i + 1 or l = j + 1) is given by:
$$ matrix[k][l] > matrix[i][j] $$
If such a path exists, print True
; otherwise, print False
.
inputFormat
The first line of input contains two integers, M and N, representing the number of rows and columns of the matrix respectively. The next M lines each contain N integers separated by spaces, representing the matrix values.
For example:
3 3 1 2 3 4 5 6 7 8 9
outputFormat
Output a single line containing either True
if there exists a strictly increasing path from the top-left to the bottom-right corner, or False
if no such path exists.
3 3
1 2 3
4 5 6
7 8 9
True