-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_rotation_algo.py
More file actions
executable file
·65 lines (56 loc) · 1.85 KB
/
matrix_rotation_algo.py
File metadata and controls
executable file
·65 lines (56 loc) · 1.85 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
59
60
61
62
63
64
65
#!/usr/bin/env python3
import os
import sys
from pathlib import Path
from typing import IO
def rotate(matrix: list[list[int]], layer: int, m: int, n: int) -> None:
"""Rotate matrix with m lines, n columns."""
tmp = matrix[layer][layer]
# left border
for i in range(layer + 1, m - layer):
tmp2 = matrix[i][layer]
matrix[i][layer] = tmp
tmp = tmp2
# bottom border
for j in range(layer + 1, n - layer):
tmp2 = matrix[m - 1 - layer][j]
matrix[m - 1 - layer][j] = tmp
tmp = tmp2
# right border
for i in range(m - 1 - layer - 1, layer - 1, -1):
tmp2 = matrix[i][n - 1 - layer]
matrix[i][n - 1 - layer] = tmp
tmp = tmp2
# top border
for j in range(n - 1 - layer - 1, layer, -1):
tmp2 = matrix[layer][j]
matrix[layer][j] = tmp
tmp = tmp2
# top left
matrix[layer][layer] = tmp
def matrixRotation(matrix: list[list[int]], r: int, m: int, n: int) -> None:
layers = min(m, n) // 2
for layer in range(layers):
steps_in_turn = (m - layer * 2 - 1) * 2 + (n - layer * 2 - 1) * 2
steps = r % steps_in_turn
for _ in range(steps):
rotate(matrix, layer, m, n)
def main(fptr: IO) -> None:
first_multiple_input = input().rstrip().split()
m = int(first_multiple_input[0])
n = int(first_multiple_input[1])
r = int(first_multiple_input[2])
matrix = []
for _ in range(m):
line = list(map(int, input().rstrip().split()))
assert len(line) == n
matrix.append(line)
matrixRotation(matrix, r, m, n)
fptr.writelines(" ".join(map(str, r)) + "\n" for r in matrix)
if __name__ == "__main__":
if path := os.getenv("OUTPUT_PATH"):
with Path(path).open("wt", encoding="utf-8") as fptr:
main(fptr)
fptr.close()
else:
main(sys.stdout)