#C2932. Find Character Paths in a Matrix

    ID: 46303 Type: Default 1000ms 256MiB

Find Character Paths in a Matrix

Find Character Paths in a Matrix

You are given a two-dimensional matrix of single characters and a target character. Your task is to identify all the occurrences of the target character in the matrix. Each occurrence is considered a valid "path", which is represented by the 0-indexed coordinate (row and column) of that cell.

Formally, you are given an m × n matrix, and a target character \(c\). Traverse the matrix in row-major order. For every cell \( (i, j) \) such that the character in the cell is equal to \(c\), output a pair \( (i, j) \). If the target character does not appear anywhere in the matrix, print -1.

Note: Even though the problem statement conceptually mentions a contiguous path that can involve moving in any direction, since the target string is of length one, a valid path consists of a single cell only.

inputFormat

The input is provided via standard input (stdin) as follows:

  • The first line contains two integers \(m\) and \(n\), representing the number of rows and columns in the matrix.
  • The next \(m\) lines each contain \(n\) space-separated characters, representing the matrix.
  • The last line contains a single character which is the target character.

outputFormat

Output the coordinates of each occurrence of the target character in the matrix, one per line. Each line should contain two integers \(i\) and \(j\) separated by a single space, representing the row and column indices (0-indexed) of a matching cell. If the target character does not appear in the matrix, output -1.

## sample
3 3
C O D
O D E
D E D
D
0 2

1 1 2 0 2 2

</p>