M, N = list(map(int,input().split()))
matrix = []
for i in range(M):
z = [int(x) for x in input().split()]
matrix.append(z)
K = int(input())
values = []
k =0
l =0
while (((N - k) > 0) and ((M - l) > 0)) and ((N - k)*(M-l)) > K:
for i in range(k,N):
values.append(matrix[k][i])
for j in range(l+1,M):
values.append(matrix[j][N-1])
for i in range(N-2,k-1,-1):
values.append(matrix[M-1][i])
for j in range(M-2,l,-1):
values.append(matrix[j][k])
values = values[-K:] + values[:-K]
c = 0
for i in range(k,N):
matrix[k][i] = values[c]
c += 1
for j in range(l+1,M):
matrix[j][N-1] = values[c]
c += 1
for i in range(N-2,k-1,-1):
matrix[M-1][i] = values[c]
c += 1
for j in range(M-2,l,-1):
matrix[j][k] = values[c]
c += 1
values = []
k += 1, l += 1, M -= 1, N -= 1
for i in matrix:
print(i)
we are getting when input is
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
but the output is:
9 10 11
5 6 12
1 7 8
when input is:
3 4
1 2 3 4
5 6 7 8
9 10 11 12
2
Comments
Leave a comment