Magic Square Classification
InterviewTime limit1sMemory limit128 MB
Read multiple square matrices and classify each into one of five magic square categories based on row, column, diagonal sums, distinctness, and consecutiveness.
- Level
Easy3 of 10
- Topics
- Matrix, Implementation, Array
- Solved
- No attempts yet
Problem
Matheos the magician wants to classify square tables of integers by their magical properties. A square has the same number of rows and columns. For each square, define the reference sum S as the sum of the first column.
Classify each square by the following rules.
Not Magick: at least one row or column does not sum toS.Semi-Magick Square: every row and every column sums toS, but at least one of the two diagonals does not.Weakly-Magick Square: every row, every column, and both diagonals sum toS.Strongly-Magick Square: every row, every column, and both diagonals sum toS, and all numbers in the square are distinct.Magically-Magick Square: every row, every column, and both diagonals sum toS; all numbers are distinct; and the numbers are consecutive integers with no gaps.
The following 2 x 2 square is a Semi-Magick Square because every row and column sums to 5, but the diagonals do not.
The following 3 x 3 square is a Magically-Magick Square because every row, column, and diagonal sums to 15, all numbers are distinct, and the numbers are consecutive from 1 through 9.
Given a sequence of squares, write a program that determines which of the five classifications applies to each square.
Input
The input consists of a sequence of squares. Each square begins with an integer n on its own line, the number of rows and columns. It is guaranteed that 2 <= n <= 8. The next n lines each contain n integers. The input ends with a line containing only 0.
Output
For each square, print one line in the form Square k: result, where k is the square number starting from 1. The result must be exactly one of the following strings.
Magically-Magick SquareStrongly-Magick SquareWeakly-Magick SquareSemi-Magick SquareNot a Magick Square