-
Notifications
You must be signed in to change notification settings - Fork 23
/
tabla.py
93 lines (75 loc) · 2.95 KB
/
tabla.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
T(4^n) = 4 T(4^n/4) + O(1)
T(k) = 4 T(k / 4) + O(1)
Linear după numărul de casetuțe pe tablă
O(4^n)
"""
fin = open('tabla.txt')
n = int(next(fin))
missing = tuple(int(x) - 1 for x in next(fin).split())
k = 2 ** n
table = [[0 for _ in range(k)] for _ in range(k)]
piece = 1
def fill(k, start, missing):
global piece
offset_i, offset_j = start
missing_i, missing_j = missing
if k == 2:
if missing_i == 0:
if missing_j == 0:
table[offset_i + 1][offset_j] = piece
table[offset_i][offset_j + 1] = piece
table[offset_i + 1][offset_j + 1] = piece
else:
table[offset_i][offset_j] = piece
table[offset_i + 1][offset_j] = piece
table[offset_i + 1][offset_j + 1] = piece
else:
if missing_j == 0:
table[offset_i][offset_j] = piece
table[offset_i][offset_j + 1] = piece
table[offset_i + 1][offset_j + 1] = piece
else:
table[offset_i][offset_j] = piece
table[offset_i][offset_j + 1] = piece
table[offset_i + 1][offset_j] = piece
piece += 1
return
mid = k // 2
# Upper half
if missing_i < mid:
# Upper left
if missing_j < mid:
fill(2, (offset_i + mid - 1, offset_j + mid - 1), (0, 0))
fill(k // 2, (offset_i, offset_j), (missing_i, missing_j))
fill(k // 2, (offset_i, offset_j + mid), (mid - 1, 0))
fill(k // 2, (offset_i + mid, offset_j), (0, mid - 1))
fill(k // 2, (offset_i + mid, offset_j + mid), (0, 0))
# Upper right
else:
fill(2, (offset_i + mid - 1, offset_j + mid - 1), (0, 1))
fill(k // 2, (offset_i, offset_j), (mid - 1, mid - 1))
fill(k // 2, (offset_i + mid, offset_j), (0, mid - 1))
fill(k // 2, (offset_i, offset_j + mid), (missing_i, missing_j - mid))
fill(k // 2, (offset_i + mid, offset_j + mid), (0, 0))
# Lower half
else:
# Lower left
if missing_j < mid:
fill(2, (offset_i + mid - 1, offset_j + mid - 1), (1, 0))
fill(k // 2, (offset_i, offset_j), (mid - 1, mid - 1))
fill(k // 2, (offset_i + mid, offset_j), (missing_i - mid, missing_j))
fill(k // 2, (offset_i, offset_j + mid), (mid - 1, 0))
fill(k // 2, (offset_i + mid, offset_j + mid), (0, 0))
# Lower right
else:
fill(2, (offset_i + mid - 1, offset_j + mid - 1), (1, 1))
fill(k // 2, (offset_i, offset_j), (mid - 1, mid - 1))
fill(k // 2, (offset_i + mid, offset_j), (0, mid - 1))
fill(k // 2, (offset_i, offset_j + mid), (mid - 1, 0))
fill(k // 2, (offset_i + mid, offset_j + mid), (missing_i - mid, missing_j - mid))
fill(k, (0, 0), missing)
for row in table:
for elem in row:
print(elem, end=' ')
print()