CC150: rotate a matrix 90 degrees
An image is represented as a matrix of n X n, and a function is written to rotate the image 90 degrees. No extra storage space
We assume that we want to rotate the image 90 degrees counter-clockwise. The source image is as follows:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The image after 90 degrees of clockwise rotation should be:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
How do we perform operations in situ to achieve the above results? You can switch symmetric elements on both sides of the main diagonal line in two steps,
Step 2 swap row I and row n-1-i to get the result. See the figure below:
Source image: After Step 1: After Step 2:
1 2 3 4 1 5 9 13 4 8 12 16
5 6 7 8 2 6 10 14 3 7 11 15
9 10 11 12 3 7 11 15 2 6 10 14
13 14 15 16 4 8 12 16 1 5 9 13
# Include
Using namespace std; void swap (int & x, int & y) {int k = x; x = y; y = k ;} void Transpote (int array [] [4], int n) {for (int I = 0; I