-
Notifications
You must be signed in to change notification settings - Fork 11
/
matrix_rotation.cpp
121 lines (96 loc) · 1.65 KB
/
matrix_rotation.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
#include<bits/stdc++.h>
#define MAX 100
using namespace std;
void fillSpiral(int mat[][MAX], int m, int n, int temp[])
{
int i, k = 0, l = 0;
int tIdx = 0;
while (k < m && l < n)
{
for (int i = l; i < n; ++i)
mat[k][i] = temp[tIdx++];
k++;
for (int i = k; i < m; ++i)
mat[i][n-1] = temp[tIdx++];
n--;
if (k < m)
{
for (int i = n-1; i >= l; --i)
mat[m-1][i] = temp[tIdx++];
m--;
}
if (l < n)
{
for (int i = m-1; i >= k; --i)
mat[i][l] = temp[tIdx++];
l++;
}
}
}
void spiralRotate(int mat[][MAX], int M, int N, int k)
{
int temp[M*N];
int m = M, n = N, s = 0, l = 0;
int *start = temp;
int tIdx = 0;
while (s < m && l < n)
{
int *end = start;
for (int i = l; i < n; ++i)
{
temp[tIdx++] = mat[s][i];
end++;
}
s++;
for (int i = s; i < m; ++i)
{
temp[tIdx++] = mat[i][n-1];
end++;
}
n--;
if (s < m)
{
for (int i = n-1; i >= l; --i)
{
temp[tIdx++] = mat[m-1][i];
end++;
}
m--;
}
if (l < n)
{
for (int i = m-1; i >= s; --i)
{
temp[tIdx++] = mat[i][l];
end++;
}
l++;
}
if (end-start > k)
{
reverse(start, start+k);
reverse(start+k, end);
reverse(start, end);
start = end;
}
else
break;
}
fillSpiral(mat, M, N, temp);
}
int main()
{
int M = 4, N = 4, k = 3;
int mat[][MAX]= {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
spiralRotate(mat, M, N, k);
for (int i=0; i<M; i++)
{
for (int j=0; j<N; j++)
cout << mat[i][j] << " ";
cout << endl;
}
return 0;
}