#C3672. Find Target in Grid

    ID: 47125 Type: Default 1000ms 256MiB

Find Target in Grid

Find Target in Grid

You are given a square grid (matrix) of integers of size (n \times n) that is sorted in non-decreasing order both row-wise and column-wise. Your task is to determine whether a given target integer exists in the grid.

If the target is found, output its position as two integers representing the row and column (using 1-based indexing). Otherwise, output -1.

If there are multiple occurrences of the target, any valid position can be returned.

For an efficient solution, consider starting from the top-right corner and moving left when the current element is greater than the target, and moving down when it is less than the target.

inputFormat

The input is read from standard input (stdin) and has the following format:

(n)
Next (n) lines: each contains (n) space-separated integers representing a row of the grid.
Last line: an integer representing the target value.

outputFormat

Print the result to standard output (stdout). If the target is found, output two space-separated integers representing the row and column (1-indexed). If not found, output -1.## sample

4
1 4 7 11
2 5 8 12
3 6 9 16
10 13 14 17
5
2 2