Rotate Matrix Rings
unknown
python
4 years ago
831 B
12
Indexable
m, n = list(map(int, input().split()))
mat = [[int(x) for x in input().split()][:n] for i in range(m)]
k = int(input())
rc = int(min(m, n) / 2 + 0.5)
for i in range(rc):
rings = []
r, c, r_offset, c_offset = i, i, 0, 1
while True:
rings.append((r, c))
if c + c_offset < i or c + c_offset > n - i - 1 or r + r_offset < i or r + r_offset > m - i - 1:
r_offset, c_offset = c_offset, -r_offset
r += r_offset
c += c_offset
if (r == i and c == i) or (c < i or c > n - i - 1 or r < i or r > m - i - 1):
break
if len(rings) <= k:
break
aux = [mat[i[0]][i[1]] for i in rings]
aux = aux[-k:] + aux[:-k]
for j in range(len(rings)):
mat[rings[j][0]][rings[j][1]] = aux[j]
for i in range(m):
print(*mat[i])
Editor is loading...