Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/python3
def get_direction(n, m, x, y):
if y == 0 and x != n - 1:
return '>', n-x-1
if y == m - 1 and x != 0:
return '<', x
if x == 0:
return '^', y
if x == n - 1:
return 'v', m-y-1
return get_direction(n-2, m-2, x-1, y-1)
def get_r0(n, m, r, x, y):
l = min(x, y, n-x-1, m-y-1)
p = (n + m - 4*l - 2) * 2
r0 = r % p
return r0
def get_rotated_coordinates(n, m, r, x, y):
r = get_r0(n, m, r, x, y)
if r == 0:
return x, y
d, steps = get_direction(n, m, x, y)
if d == '<':
if steps >= r:
return x - r, y
else:
return get_rotated_coordinates(n, m, r - steps, x - steps, y)
if d == '>':
if steps >= r:
return x + r, y
else:
return get_rotated_coordinates(n, m, r - steps, x + steps, y)
if d == 'v':
if r <= steps:
return x, y + r
else:
return get_rotated_coordinates(n, m, r - steps, x, y + steps)
if d == '^':
if steps >= r:
return x, y - r
else:
return get_rotated_coordinates(n, m, r - steps, x, y - steps)
if __name__ == '__main__':
mnr = input().rstrip().split()
m, n, r = map(int, mnr)
matrix = []
for _ in range(m):
matrix.append(list(map(str, input().rstrip().split())))
# Print direction matrix:
# for y in range(m):
# for x in range(n):
# d, s = get_direction(n, m, x, y)
# print(f'{d}{s}', end=' ')
# print()
# print()
for y in range(m):
for x in range(n):
xr, yr = get_rotated_coordinates(n, m, r, x, y)
print(matrix[yr][xr], end=' ')
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment