#C7415. Local Maxima Finder in a Matrix

    ID: 51284 Type: Default 1000ms 256MiB

Local Maxima Finder in a Matrix

Local Maxima Finder in a Matrix

Given an \( M \times N \) matrix filled with positive integers, a cell \( (i,j) \) is defined as a local maximum if its value is greater than or equal to all of its adjacent neighbors (up to 8 neighbors, with fewer on the boundaries). Your task is to find the coordinates of all such local maxima.

Formally, for each cell \( (i,j) \) in the matrix, it is a local maximum if:

\( matrix[i][j] \geq matrix[i+di][j+dj] \) for all \( di,dj \in \{-1,0,1\} \) with \( (di,dj) \neq (0,0) \) and valid indices.

The output should list the total number of local maxima, followed by their coordinates sorted in ascending order first by row index and then by column index.

inputFormat

The input is read from stdin and is structured as follows:

  • The first line contains two integers \( M \) and \( N \), representing the number of rows and columns of the matrix.
  • The following \( M \) lines each contain \( N \) space-separated integers representing the matrix.

outputFormat

The output is written to stdout and should be in the following format:

  • The first line contains a single integer denoting the number of local maxima found.
  • Each subsequent line contains the coordinates of a local maximum in the format \( (row, column) \).
  • The coordinates must be listed in sorted order (first by row, then by column).
## sample
3 3
1 2 1
2 3 2
1 2 1
1

(1, 1)

</p>