-
Notifications
You must be signed in to change notification settings - Fork 175
/
magic-square-forming.py
63 lines (50 loc) · 1.34 KB
/
magic-square-forming.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/python3
import math
import os
import random
import re
import sys
def is_magic(s):
tmp = [
s[0] + s[1] + s[2],
s[3] + s[4] + s[5],
s[6] + s[7] + s[8],
s[0] + s[3] + s[6],
s[1] + s[4] + s[7],
s[2] + s[5] + s[8],
s[0] + s[4] + s[8],
s[2] + s[4] + s[6],
]
return all(x == tmp[0] for x in tmp)
def cost(s1, s2):
return sum(abs(a - b) for a, b in zip(s1, s2))
#
# Complete the 'formingMagicSquare' function below.
#
# The function is expected to return an INTEGER.
# The function accepts 2D_INTEGER_ARRAY s as parameter.
#
def formingMagicSquare(s):
s = s[0] + s[1] + s[2]
ss = [None for _ in range(9)]
a = [True for _ in range(10)]
def rec(i, best):
if i == 9 and is_magic(ss):
return cost(s, ss)
else:
for j in range(1, 10):
if a[j]:
a[j] = False
ss[i] = j
best = min(best, rec(i + 1, best))
a[j] = True
return best
return rec(0, sys.maxsize)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = []
for _ in range(3):
s.append(list(map(int, input().rstrip().split())))
result = formingMagicSquare(s)
fptr.write(str(result) + '\n')
fptr.close()