2021年4月4日星期日

matrix of order M*N and a value K, each ring of the matrix clockwise by K elements

Rotate Matrix Rings

Given a matrix of order M*N and a value K, write a program to rotate each ring of the matrix clockwise by K elements. If in any ring has less than or equal to K elements, then don't rotate that ring.

Input

The first line of input will be two space-separated integers, denoting the M and N. The next M lines will contain N space-separated integers. The next line will contain an integer, denoting K.

Output

The output should be M*N matrix by rotating the matrix by K elements.

Explanation

For example, if the given M and N are 4 and 4 respectively. If the matrix elements are 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 If the given K is 3. Rotate each ring of the matrix by 3 elements. In the above matrix, the elements (1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5) is a ring, similarly, the elements (6, 7, 11, 10) will make a ring. Therefore, by rotating each ring in clockwise direction by 3 elements will give (13, 9, 5, 1, 2, 3, 4, 8, 12, 16, 15, 14) and (10, 6, 7, 11). So the output should be 13 9 5 1 14 7 11 2 15 6 10 3 16 12 8 4

https://stackoverflow.com/questions/66947798/matrix-of-order-mn-and-a-value-k-each-ring-of-the-matrix-clockwise-by-k-elemen April 05, 2021 at 11:06AM

没有评论:

发表评论