#C5747. Magic Square Verification
Magic Square Verification
Magic Square Verification
In this problem, you are given a square grid of integers. Your task is to determine whether the grid is a magic square or not.
A grid is considered a magic square if:
- All rows sum to the same value.
- All columns sum to the same value.
- The main diagonal (top-left to bottom-right) and the secondary diagonal (top-right to bottom-left) also sum to that same value.
If the grid is empty, it is not considered a magic square. Note that a single element grid is always magic.
Input and Output via standard input (stdin) and standard output (stdout) are required.
inputFormat
The input begins with an integer n
representing the number of rows (and columns) in the grid. If n = 0
, it means the grid is empty.
If n > 0
, the next n
lines each contain n
integers separated by spaces representing a row of the grid.
For example:
3 2 7 6 9 5 1 4 3 8
outputFormat
Output a single line: YES
if the grid is a magic square and NO
otherwise.
3
2 7 6
9 5 1
4 3 8
YES