-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRotate Matrix.cpp
More file actions
45 lines (34 loc) · 1.14 KB
/
Rotate Matrix.cpp
File metadata and controls
45 lines (34 loc) · 1.14 KB
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
// Invertendo Matrizes 90º
//
// 90 graus anti-horario
void rotateMatrix(vector<vector<int>>& mat, int N) {
for (int x = 0; x < N / 2; x++) {
for (int y = x; y < N - x - 1; y++) {
int temp = mat[x][y];
// direita → topo
mat[x][y] = mat[y][N - 1 - x];
// fundo → direita
mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y];
// esquerda → fundo
mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x];
// topo (temp) → esquerda
mat[N - 1 - y][x] = temp;
}
}
}
// 90 graus horario (ou 3x anti-horario)
void rotateMatrixCW(vector<vector<int>>& mat, int N) {
for (int x = 0; x < N / 2; x++) {
for (int y = x; y < N - x - 1; y++) {
int temp = mat[x][y];
// esquerda → topo
mat[x][y] = mat[N - 1 - y][x];
// fundo → esquerda
mat[N - 1 - y][x] = mat[N - 1 - x][N - 1 - y];
// direita → fundo
mat[N - 1 - x][N - 1 - y] = mat[y][N - 1 - x];
// topo (temp) → direita
mat[y][N - 1 - x] = temp;
}
}
}