DMOPC '23 Contest 1 P1 - Peak Grid

View as PDF

Submit solution

Points: 5 (partial)
Time limit: 2.0s
Memory limit: 1G

Problem types

Aschin is interested in the peaks of a grid. A peak is defined as an element of the grid that is the largest in both the row and column containing that element. Please impress Aschin by constructing an N by N grid of distinct integers from 1 to N^2 such that it has exactly K peaks.


1 \le N \le 2 \times 10^3

1 \le K \le N

Subtask 1 [30%]

1 \le N \le 3

Subtask 2 [70%]

No additional constraints.

Input Specification

The first and only line contains the space-separated integers N and K.

Output Specification

Output N lines containing N space-separated integers, a grid of size N by N which contains each integer from 1 to N^2 such that there are exactly K peaks. If there are multiple solutions, output any of them. If such a grid does not exist, output -1.

Sample Input

3 2

Sample Output

9 1 2
8 7 5
4 3 6

Explanation for Sample

6 and 9 are the 2 peaks.


There are no comments at the moment.