You are given a 2D matrix, a, of dimension MxN and a positive integer R. You have to rotate the matrix R times and print the resultant matrix. Rotation should be in anti-clockwise direction. Note that in one rotation, you have to shift elements by one step only (refer sample tests for more clarity).
It is guaranteed that the minimum of M and N will be even.
First line contains three space separated integers, M, N and R, where M is the number of rows, N is number of columns in matrix, and R is the number of times the matrix has to be rotated. Then M lines follow, where each line contains N space separated positive integers. These M lines represent the matrix.
Print the rotated matrix.
2 <= M, N <= 300
1 <= R <= 10^9
min(M, N) % 2 == 0
1 <= a(i,j) <= 10^8, where i is [1..M] & j is [1..N]
4 4 1
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2 3 4 8
1 7 11 12
5 6 10 16
9 13 14 15
4 4 2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3 4 8 12
2 11 10 16
1 7 6 15
5 9 13 14
5 4 7
1 2 3 4
7 8 9 10
13 14 15 16
19 20 21 22
25 26 27 28
28 27 26 25
22 9 15 19
16 8 21 13
10 14 20 7
4 3 2 1
2 2 3
1 1
1 1
1 1
1 1
Sample Case #00: Here is an illustration of what happens when the matrix is rotated once.
1 2 3 4 2 3 4 8
5 6 7 8 1 7 11 12
9 10 11 12 -> 5 6 10 16
13 14 15 16 9 13 14 15
Sample Case #01: Here is what happens when to the matrix after two rotations.
1 2 3 4 2 3 4 8 3 4 8 12
5 6 7 8 1 7 11 12 2 11 10 16
9 10 11 12 -> 5 6 10 16 -> 1 7 6 15
13 14 15 16 9 13 14 15 5 9 13 14
Sample Case #02: Following are the intermediate states.
1 2 3 4 2 3 4 10 3 4 10 16 4 10 16 22
7 8 9 10 1 9 15 16 2 15 21 22 3 21 20 28
13 14 15 16 -> 7 8 21 22 -> 1 9 20 28 -> 2 15 14 27 ->
19 20 21 22 13 14 20 28 7 8 14 27 1 9 8 26
25 26 27 28 19 25 26 27 13 19 25 26 7 13 19 25
10 16 22 28 16 22 28 27 22 28 27 26 28 27 26 25
4 20 14 27 10 14 8 26 16 8 9 25 22 9 15 19
3 21 8 26 -> 4 20 9 25 -> 10 14 15 19 -> 16 8 21 13
2 15 9 25 3 21 15 19 4 20 21 13 10 14 20 7
1 7 13 19 2 1 7 13 3 2 1 7 4 3 2 1
Sample Case #03: As all elements are same, any rotation will reflect the same matrix.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int l, m, Row, Col, mat[300][300];
void rotation(int l, int m, int Row, int Col) {
int si, sj, i, j, t, f;
si = l;
sj = m;
t = mat[l][m];
for (i = l + 1; i <= Row; i++) {
f = mat[i][m];
mat[i][m] = t;
t = f;
}
l++;
for (i = m + 1; i <= Col; i++) {
f = mat[Row][i];
mat[Row][i] = t;
t = f;
}
m++;
if (l - 1 < Row) {
for (i = Row - 1; i >= l - 1; i--) {
f = mat[i][Col];
mat[i][Col] = t;
t = f;
}
}
Col--;
if (m - 1 < Col) {
for (i = Col; i >= m; i--) {
f = mat[l - 1][i];
mat[l - 1][i] = t;
t = f;
}
}
Row--;
mat[si][sj] = t;
return;
}
int main() {
int M, N, R, i, j;
scanf("%d%d%d", &M, &N, &R);
int f, K;
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
scanf("%d", &mat[i][j]);
}
}
l = 0;
m = 0;
Row = M - 1;
Col = N - 1;
while (l < Row && m < Col) {
int rot = 2 * (Row - l) + 2 * (Col - m);
f = k % rot;
for (i = 1; i <= f; i++) {
rotation(l, m, Row, Col);
}
l++;
m++;
Row--;
Col--;
}
for (i = 0; i < M; i++) {
for (j = 0; j < N; j++) {
printf("%d ", mat[i][j]);
}
printf("\n");
}
return 0;
}