-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrot.cpp
95 lines (93 loc) · 3.23 KB
/
matrot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
using namespace std;
// Main algorithm: in order to rotate the whole matrix, we'll just rotate one ring at a time
// We can do this in-place to achieve O(1) additional space complexity
int main() {
int n, m, k;
char s;
cin>>k;
cin>>n>>s>>m;
int **matrix = new int*[n];
for(int i = 0; i < n; i++) {
matrix[i] = new int[m];
for(int j = 0; j < m; j++) {
cin>>matrix[i][j];
if(j<m-1){
cin>>s;
}
}
}
int numRings = min(n,m)/2;
for(int i = 0; i < numRings; i++) {
int numRotations = k%(2*(n + m - 4*i) - 4);
if(i%2 == 0){
//Rotate anticlockwise direction
for(int rotation = 0; rotation < numRotations; rotation++) {
// Rotate top row
for(int j = i; j < m-i-1; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][j+1];
matrix[i][j+1] = tmp;
}
// Rotate right column
for(int j = i; j < n-i-1; j++) {
int tmp = matrix[j][m-i-1];
matrix[j][m-i-1] = matrix[j+1][m-i-1];
matrix[j+1][m-i-1] = tmp;
}
// Rotate bottom row
for(int j = m-i-1; j > i; j--) {
int tmp = matrix[n-i-1][j];
matrix[n-i-1][j] = matrix[n-i-1][j-1];
matrix[n-i-1][j-1] = tmp;
}
// Rotate left column
for(int j = n-i-1; j > i+1; j--) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[j-1][i];
matrix[j-1][i] = tmp;
}
}
}
else{
//Rotate clockwise direction
for(int rotation = 0; rotation < numRotations; rotation++) {
//Rotate Top row
for(int j = m-i-1; j > i; j--) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[i][j-1];
matrix[i][j-1] = tmp;
}
// Rotate left column
for(int j = i; j < n-i-1; j++) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[j+1][i];
matrix[j+1][i] = tmp;
}
// Rotate bottom row
for(int j = i; j < m-i-1; j++) {
int tmp = matrix[n-i-1][j];
matrix[n-i-1][j] = matrix[n-i-1][j+1];
matrix[n-i-1][j+1] = tmp;
}
// Rotate right column
for(int j = n-i-1; j > i+1; j--) {
int tmp = matrix[j][m-i-1];
matrix[j][m-i-1] = matrix[j-1][m-i-1];
matrix[j-1][m-i-1] = tmp;
}
}
}
}
// Output final matrix
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout<<matrix[i][j];
if(j<m-1){
cout<<s;
}
}
cout<<"\n";
}
return 0;
}