-
Notifications
You must be signed in to change notification settings - Fork 1
/
32-rotate-by-90-degree.cpp
59 lines (55 loc) · 1.22 KB
/
32-rotate-by-90-degree.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
#include<bits/stdc++.h>
using namespace std;
void rotate (vector<vector<int> >& matrix);
/* matrix : given input matrix, you are require
to change it in place without using extra space */
void rotate(vector<vector<int> >& matrix)
{
int n = matrix.size();
int k = 1;
for(int i = 0; i < n; ++i)
{
for(int j = k; j < n; ++j)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
k++;
}
for(int i = 0; i < (n / 2); ++i)
{
for(int j = 0; j < n; ++j)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[n - i - 1][j];
matrix[n - i - 1][j] = temp;
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<vector<int> > matrix(n);
for(int i=0; i<n; i++)
{
matrix[i].resize(n);
for(int j=0; j<n; j++)
cin>>matrix[i][j];
}
rotate(matrix);
for (int i = 0; i < n; ++i)
{
for(int j=0; j<n; j++)
cout<<matrix[i][j]<<" ";
cout<<"\n";
}
}
return 0;
}
// } Driver Code Ends