-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotate_matrix.cpp
More file actions
64 lines (54 loc) · 1.18 KB
/
rotate_matrix.cpp
File metadata and controls
64 lines (54 loc) · 1.18 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
using namespace std;
bool rotate(int **matrix,int len){
if(len == 0){
//((matrix.sizeof())/(matrix[0].sizeof()))){
return false;
}
for(int layer=0; layer<len/2; layer++){
cout << "layer " << layer << endl;
int first=layer;
int last=len-1-layer;
for(int i=first; i<last; i++){
int offset = i-first;
int temp;
printf("offset %d\n", offset);
//keep the top
temp = matrix[first][i];
//left to top;
matrix[first][i] = matrix[last-offset][first];
//bottom to left
matrix[last-offset][first] = matrix[last][last-offset];
//right to bottom
matrix[last][last-offset] = matrix[i][last];
//temp to right
matrix[i][last] = temp;
}
}
return true;
}
int main(){
int **matrix;
int len = 4;
matrix = new int *[len];
for (int i = 0; i < len; i++) {
matrix[i] = new int[len];
for (int j = 0; j < len; j++) {
matrix[i][j] = i * len + j;
}
}
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
rotate(matrix,len);
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
return 0;
}