-
Notifications
You must be signed in to change notification settings - Fork 8
/
MatrixLayerRotation.cpp
123 lines (101 loc) · 2.31 KB
/
MatrixLayerRotation.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<bits/stdc++.h>
#define ll long long
using namespace std;
vector<ll> update(vector<ll>v,ll rot){
ll s = v.size();
vector<ll>v2;
for(ll i=s-rot;i<s;i++){
v2.push_back(v[i]);
}
for(ll i=0;i<s-rot;i++){
v2.push_back(v[i]);
}
return v2;
}
int main()
{
ll m,n,rotations,r,c,x=0;
cin>>m>>n>>rotations;
ll** mat = new ll*[m];
for(ll i=0;i<m;i++){
mat[i] = new ll[n];
}
for(ll i=0;i<m;i++){
for(ll j=0;j<n;j++){
cin>>mat[i][j];
}
}
ll k = (min(m,n))/2;
for(ll i=0;i<k;i++){
vector<ll>v;
//1st
c = i;
for(ll r=i;r<m-x;r++){
v.push_back(mat[r][c]);
}
//2nd
r = m - 1 - x;
for(ll c=i+1;c<n-x;c++){
v.push_back(mat[r][c]);
}
//3rd
c = n - 1 - x;
for(ll r=m-1-x-1;r>=i;r--){
v.push_back(mat[r][c]);
}
//4th
r = i;
for(ll c=n-1-x-1;c>i;c--){
v.push_back(mat[r][c]);
}
//Optimise Rotations
ll start_row = i;
ll start_col = i;
ll end_row = m - 1 - x;
ll end_col = n - 1 - x;
ll mod = 2*(end_col-start_col+end_row-start_row);
ll rot = rotations%mod;
//Initial Vector
// for(ll i=0;i<v.size();i++){
// cout<<v[i]<<" ";
// }
// cout<<endl;
//Update the vector
vector<ll>temp = update(v,rot);
ll ptr = 0;
//Update the matrix
//1st
c = i;
for(ll r=i;r<m-x;r++){
mat[r][c]=temp[ptr]; ptr++;
}
//2nd
r = m - 1 - x;
for(ll c=i+1;c<n-x;c++){
mat[r][c]=temp[ptr]; ptr++;
}
//3rd
c = n - 1 - x;
for(ll r=m-1-x-1;r>=i;r--){
mat[r][c]=temp[ptr]; ptr++;
}
//4th
r = i;
for(ll c=n-1-x-1;c>i;c--){
mat[r][c]=temp[ptr]; ptr++;
}
//Updated Vector
// for(ll i=0;i<temp.size();i++){
// cout<<temp[i]<<" ";
// }
// cout<<endl;
x++;
}
for(ll i=0;i<m;i++){
for(ll j=0;j<n;j++){
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
return 0;
}