#K92672. Rotational Magic Square Verification
Rotational Magic Square Verification
Rotational Magic Square Verification
You are given an odd integer n
and an n x n
grid filled with integers. A magic square is a square matrix in which the sum of every row, every column, and both main diagonals are equal.
Your task is to determine whether the given grid is a magic square. In the problem statement, it is mentioned that you may rotate any row or column in a cyclic manner. However, for the purpose of this problem, you only need to check whether the grid already is a magic square (i.e. no rotations are required).
For a magic square of size n
, if we denote the grid as \(a_{ij}\) for \(0 \leq i,j < n\), then there exists a constant \(S\) such that:
\[
\sum_{j=0}^{n-1} a_{ij} = S, \quad \sum_{i=0}^{n-1} a_{ij} = S, \quad \sum_{i=0}^{n-1} a_{ii} = S, \quad \sum_{i=0}^{n-1} a_{i,n-1-i} = S
\]
Return "YES" if the grid is a magic square, otherwise return "NO".
inputFormat
The input is read from stdin and has the following format:
The first line contains an integern
(the size of the grid). The followingn
lines each containn
space-separated integers representing a row of the grid.
outputFormat
Output a single line to stdout containing "YES" if the input grid is a magic square, or "NO" otherwise.
## sample3
8 1 6
3 5 7
4 9 2
YES