Fortune left-law basis class3- topic 6 square rotation matrix

Fortune left-law basis class3- topic 6 square rotation matrix

1. Topic

[Title] Given an integer matrix square matrix, the matrix is adjusted to make the
clockwise rotation of 90 degrees like.
[] Required extra space complexity is O (1), without using an auxiliary array.

Here Insert Picture Description

2. Analysis

Analysis: Title given square matrix, consider the structure of the matrix back to shape as to be rotated by layer from outside to inside, after the first outer layer. Thought the same subject 5 with left and right corners of a predetermined point (a, b) (c, d). Because you can not use an auxiliary array, the number of rotation of the cover before it needs a temp variable staging of a number.
(1) After a temporary number by a, b, c, d of the four variables 1,4,16,13 four overwrites the previous count by one in the order of black arrows;
(2) a pair of rows 2,8,15,9 direction and for sequentially (1) a step similar to spin coating, 3,12,14,5, too, so that the number of rotation of the outermost layer 12 is completed;
(. 3) a ++, B ++, the C- , the same for the inner layer 6,7,11,10 d-.
Tips : each outermost row number 4, there are three starting 1,2,3, 2 per two lines is only a starting point for the inner layer 6, then the number represents the starting point need db i.e. how much each cycle times. The title outer 12 can rotate three times the number of cycles, the number of cycles once the inner four.

Here Insert Picture Description

3. core code

(1) initializes a, b, c, d.

int a = 0,b = 0,c = height - 1,d = width - 1;

(2) rotating each code, by the position i and a, b, c, d of the control array, the simple ability to inspect the code

void rotate_matrix(int arr[][width],int a,int b,int c,int d)
{
	int times = d - b;
		for(int i = 0;i < times;i++)
		{
			int temp = arr[a][b + i];
			arr[a][b + i] = arr[c - i][b];
			arr[c - i][b] = arr[c][d - i];
			arr[c][d - i] = arr[a + i][d];
			arr[a + i][d] = temp;
		}
}

(3) total number of layers required to control the rotation by a, c two variables. Left and right corners of each point of a grid after rotation gradually moves toward the center, because the subject is given a square, then the two points coincide only possible swash wrong abscissa two points a, c control the rotating layer number.

while(a < c)
	{
		rotate_matrix(arr,a++,b++,c--,d--);
	}

4. The complete code

#include<iostream>
#define height 4
#define width 4
using namespace std;

void rotate_matrix(int arr[][width],int a,int b,int c,int d)
{
	int times = d - b;
		for(int i = 0;i < times;i++)
		{
			int temp = arr[a][b + i];
			arr[a][b + i] = arr[c - i][b];
			arr[c - i][b] = arr[c][d - i];
			arr[c][d - i] = arr[a + i][d];
			arr[a + i][d] = temp;
		}
}

void print(int arr[][width])
{
	//打印输出
	for(int i = 0;i < height;i++)
	{
		for(int j = 0;j < width;j++)
		{
			cout<<arr[i][j]<<" " ;
		}
		cout<<endl;
	}
}
int main()
{
	//int arr[height][width] = {1,2,3,4,5,6,7,8,9};
	int arr[height][width] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
	int a = 0,b = 0,c = height - 1,d = width - 1;
	
	while(a < c)
	{
		rotate_matrix(arr,a++,b++,c--,d--);
	}
	print(arr);

 	system("pause");
	return 0;
}

5. The output

Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1387

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103660593