#C9228. Retained Files in a Grid

    ID: 53298 Type: Default 1000ms 256MiB

Retained Files in a Grid

Retained Files in a Grid

You are given a computer system in which files are arranged in a grid format. The grid has n rows and m columns, making a total of n \times m files. Each file is uniquely identified by a pair of indices \((i, j)\) where \(1 \le i \le n\) and \(1 \le j \le m\).

You are also given a central file located at coordinates \((a, b)\) and a positive integer \(r\) which represents the maximum retain distance. A file located at \((i, j)\) is retained if its Manhattan distance from the central file is at most \(r\). The Manhattan distance between \((i_1, j_1)\) and \((i_2, j_2)\) is defined as:

[ |i_1 - a| + |j_1 - b| \le r ]

Your task is to determine which files are retained. The retained files should be printed in row-major order (i.e. sorted first by row then by column).

Note: The Manhattan distance is given by \[ |i - a| + |j - b| \]

inputFormat

The input consists of a single line containing five space-separated integers: n, m, r, a, and b, where:

  • n is the number of rows of the grid.
  • m is the number of columns of the grid.
  • r is the maximum Manhattan distance for a file to be retained.
  • a and b specify the row and column of the central file respectively.

outputFormat

Output to stdout. The first line should contain the total number of retained files. Each of the following lines should contain two integers representing the row and column of a retained file. The files must be listed in row-major order (sorted first by row, then by column).

## sample
5 5 2 3 3
13

1 3 2 2 2 3 2 4 3 1 3 2 3 3 3 4 3 5 4 2 4 3 4 4 5 3

</p>