Created
May 16, 2017 04:22
-
-
Save takanakahiko/60c65d1f7a66c367cc4f4c6fb41c0781 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
import copy | |
N = int(raw_input()) | |
num_list = list() | |
less_num_list = list() | |
less_pos_list = list() | |
for i in range(N): | |
a = raw_input().split() | |
num_list.append(map(int,a)) | |
for j in range(N): | |
if num_list[i][j] == 0: | |
less_pos_list.append([i,j]) | |
flatten = [flatten for inner in num_list for flatten in inner] | |
for i in range(N**2 + 1): | |
if flatten.count(i) == 0 : | |
less_num_list.append(i) | |
num_permutations = list(itertools.permutations(less_num_list)) | |
num_list_tmp = copy.deepcopy(num_list) | |
try: | |
for count in range(len(num_permutations)): | |
isOK = True | |
num_list_tmp = copy.deepcopy(num_list) | |
for seq in range(len(less_pos_list)): | |
set_num = num_permutations[count][seq] | |
num_list_tmp[less_pos_list[seq][0]][less_pos_list[seq][1]] = set_num | |
sample = sum(num_list_tmp[0]) | |
naname1 = 0 | |
naname2 = 0 | |
for i in range(N): | |
if sum(num_list_tmp[i]) != sample: | |
isOK = False | |
sum0 = 0 | |
for j in range(N): | |
sum0 += num_list_tmp[j][i] | |
if sum0 != sample: | |
isOK = False | |
naname1 += num_list_tmp[i][N-i-1] | |
naname2 += num_list_tmp[N-i-1][i] | |
if naname1 != sample: | |
print count | |
isOK = False | |
if naname2 != sample: | |
isOK = False | |
if isOK == True: | |
raise Exception | |
except Exception: | |
pass | |
for i in num_list_tmp: | |
a = ' '.join(map(str,i)) | |
print a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment