-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_matrix.py
More file actions
58 lines (47 loc) · 1.15 KB
/
spiral_matrix.py
File metadata and controls
58 lines (47 loc) · 1.15 KB
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
inp = input().strip().split(sep=' ')
n_ = int(inp[0])
m_ = int(inp[1])
tmp_matr = []
for i in range(n_):
tmp_str = []
for j in range(m_):
tmp_str.append(0)
tmp_matr.append(tmp_str)
n_i = 0
n_j = 1
tmp_i = 0
tmp_j = 0
for k in range(n_*m_):
tmp_matr[tmp_i][tmp_j] = k+1
if n_j == 1 and n_i == 0:
if tmp_j + n_j == m_:
n_i = 1
n_j = 0
elif tmp_matr[tmp_i][tmp_j + n_j] != 0:
n_i = 1
n_j = 0
elif n_j == -1 and n_i == 0:
if tmp_j + n_j < 0:
n_i = -1
n_j = 0
elif tmp_matr[tmp_i][tmp_j + n_j] != 0:
n_i = -1
n_j = 0
elif n_i == 1 and n_j == 0:
if tmp_i + n_i == n_:
n_i = 0
n_j = -1
elif tmp_matr[tmp_i + n_i][tmp_j] != 0:
n_i = 0
n_j = -1
elif n_i == -1 and n_j == 0:
if tmp_i + n_i == 0:
n_i = 0
n_j = 1
elif tmp_matr[tmp_i + n_i][tmp_j] != 0:
n_i = 0
n_j = 1
tmp_i += n_i
tmp_j += n_j
for i in range(n_):
print(' '.join([str(num) for num in tmp_matr[i]]))