#K5926. Uniform Matrix Path

    ID: 30825 Type: Default 1000ms 256MiB

Uniform Matrix Path

Uniform Matrix Path

You are given an \(N \times M\) matrix of integers. Your task is to determine if there exists a path from the top-left corner to the bottom-right corner such that all cells along the path have the same value. You are only allowed to move either right or down at each step.

Formally, let the matrix be denoted by \(A\) where \(A_{i,j}\) is the value at row \(i\) and column \(j\) (using 0-based indexing). A valid path \(P\) is a sequence of coordinates \((i_0, j_0), (i_1, j_1), \dots, (i_k, j_k)\) such that:

  • \((i_0, j_0) = (0,0)\) and \((i_k, j_k) = (N-1, M-1)\).
  • For every consecutive pair \((i, j)\) and \((i', j')\) in \(P\), either \(i' = i + 1\) and \(j' = j\) (move down) or \(i' = i\) and \(j' = j + 1\) (move right).
  • All cells \(A_{i,j}\) along the path have the same value.

Output \(True\) if such a path exists and \(False\) otherwise.

inputFormat

The input is given via standard input. The first line contains two integers \(N\) and \(M\) separated by a space, representing the number of rows and columns of the matrix, respectively. This is followed by \(N\) lines, each containing \(M\) integers separated by spaces, representing the rows of the matrix.

outputFormat

Output a single line to standard output containing either True or False (case-sensitive), indicating whether there exists a uniform path as described in the problem statement.

## sample
3 3
1 1 1
2 1 2
2 1 1
True