Editorial for CCC '16 J2 - Magic Squares


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.

Sum any row or column, and then check if the sum of the remaining rows and columns are identical.

Time Complexity: \mathcal{O}(1)

Solution — Python 2

S = [[int(s) for s in raw_input().split()] for _ in xrange(4)]
M = True
T = sum([S[0][j] for j in xrange(4)])
for i in xrange(4):
    M &= (T == sum([S[i][j] for j in xrange(4)]))
for i in xrange(4):
    M &= (T == sum([S[j][i] for j in xrange(4)]))
print 'magic' if M else 'not magic'

Comments

There are no comments at the moment.