atcoder#ARC142B. [ARC142B] Unbalanced Squares

[ARC142B] Unbalanced Squares

Score : 400400 points

Problem Statement

We have an N×NN \times N grid. Let (i,j)(i,j) denote the square at the ii-th row from the top and jj-th column from the left in this grid. Find one way to write an integer on every square to satisfy the conditions below.

  • Each integer between 11 and N2N^2 is written exactly once.
  • For every pair of integers i,j(1i,jN)i,j\, (1 \leq i,j \leq N), the square (i,j)(i,j) satisfies the following.- Among the squares horizontally, vertically, or diagonally adjacent to (i,j)(i,j) (there are at most eight of them), let aa and bb be the number of squares with integers larger than and smaller than that of (i,j)(i,j), respectively. Then, aba \neq b holds.
  • Among the squares horizontally, vertically, or diagonally adjacent to (i,j)(i,j) (there are at most eight of them), let aa and bb be the number of squares with integers larger than and smaller than that of (i,j)(i,j), respectively. Then, aba \neq b holds.

Under the Constraints of this problem, it can be proved that such a way to write integers always exists.

Constraints

  • 2N5002 \leq N \leq 500
  • NN is an integer.

Input

Input is given from Standard Input in the following format:

NN

Output

Print a way to write integers to satisfy the conditions, in the following format:

x1,1x_{1,1} \ldots x1,Nx_{1,N}

\vdots

xN,1x_{N,1} \ldots xN,Nx_{N,N}

Here, xi,jx_{i,j} is the integer to write on the square (i,j)(i,j). If multiple solutions exist, any of them will be accepted.

2
1 2
3 4

This output contains each integer between 11 and N2(=4)N^2\, (=4) exactly once, so the first condition is satisfied. Additionally, among the squares horizontally, vertically, or diagonally adjacent to the square (1,1)(1,1), three squares (1,2)(1,2), (2,1)(2,1), and (2,2)(2,2) have integers larger than that of (1,1)(1,1), and none has an integer smaller than that. Thus, for (1,1)(1,1), we have a=3a=3 and b=0b=0, so aba\neq b holds. Similarly, it can be verified that aba\neq b also holds for the other squares, so the second condition is satisfied. Therefore, this output is valid.

3
1 2 3
5 4 6
7 8 9