Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import List
- from typing import Set
- def f(cnt: List[int], s: Set[int], pos: int, res_matrix: List[int], n: int) -> bool:
- if pos >= n*n:
- for i in range(0, n):
- dif_row = 0
- for j in range(0, n):
- if (j % 2 == 0):
- dif_row += res_matrix[i*n+j]
- else:
- dif_row -= res_matrix[i*n+j]
- if dif_row % 11 != 0:
- return False
- for j in range(0, n):
- dif_col = 0
- for i in range(0, n):
- if (i%2 == 0):
- dif_col += res_matrix[i*n+j]
- else:
- dif_col -= res_matrix[i*n+j]
- if dif_col % 11 != 0:
- return False
- for i in range(0, n):
- for j in range(0, n):
- print(res_matrix[i*n+j], end=' ')
- print()
- return True
- else:
- for i in s:
- res_matrix[pos] = i
- cnt[i] -= 1
- if cnt[i] == 0:
- s.remove(i)
- if f(cnt, s, pos+1, res_matrix, n):
- return True
- cnt[i] += 1
- if cnt[i] == 1:
- s.add(i)
- return False
- n = int(input())
- a = list(map(int, input().split()))
- cnt = [0] * 10
- s = set()
- for i in a:
- if cnt[i] == 0:
- s.add(i)
- cnt[i] += 1
- if not f(cnt, s, 0, [0]*(n*n), n):
- print("Opps, looks like it's impossible :(")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement