[Matrix] spiral Matrix problem (bottom)

Source: Internet
Author: User

Then the previous article:

[Matrix] spiral Matrix problem (top): http://blog.csdn.net/beyongwang/article/details/52089951

Online to see a more general understanding of the algorithm (for reference: http://www.cnblogs.com/eshizhan/archive/2010/06/01/1749013.html), combined with this idea of a detailed analysis of the spiral matrix problem, And how to flexibly change the direction of the spiral and the order of arrangement.

First look at the 5*5 spiral Matrix, which has a clockwise inner-to-outer:

If the matrix is placed in a coordinate system, the direction of the row and column of the two-dimensional array grows: The x-axis increases in the right direction and the y-axis increases. The effect is as follows (each element/lattice represents a point, not every line, so the following figure is the coordinates 0 to 4,x and the y-axis each have five points):

If the matrix is centered on the center point, the matrix is divided into four parts, to observe the changes in the X, Y, you can see that the above part is an increase in X., the following part is the X reduction, the left part is Y decrease, the right part is Y increase:

Continuing to observe the two dividing lines, you can find that they are actually two linear equations x = 4-y and x = Y, while the four parts satisfy different conditions (first regardless of the point just in the straight line situation, will be said later), as follows: part: x > Y and x < 4-y. Two parts: x > Y and x > 4-y. Three parts: x < Y and x > 4-y. Four parts: x < Y and x < 4-y.

Continue to analyze the situation where the points fall in the linear equation, take a look at the figure below to 3,5,7,9 four points for example, for 3 points, its next step is left (--x) so it belongs to the region three, a similar inference method, 5 belongs to region four, 7 belongs to region One, 9 belongs to region one, So consider the situation where the point falls to the line. The conditions for four regions are: part: x >= Y and x <= 4-y. Two parts: x > Y and x > 4-y. Three parts: x <= Y and x > 4-y. Four parts: x < Y and x <= 4-y.

Analysis of the above mechanism can be written code, the code is as follows, specific reference notes:

vector<vector<int>>  Generatespiralmatrixi (int n) {
	vector<vector<int>> retVec (n, Vector<int> (n, 0));

	if (0 = = N)//Boundary condition judgment
	{
		return retvec;
	}

	int x = N/2;
	int y = N/2;
	for (size_t index = 1; index <= n * n; ++index)//outer loop, which controls the element contents to be filled
	{
		if (x >= y && x <= n-1-y )//The element falls into the upper image area one
		{
			retvec[y][x] = index;//element Seated
			++x;//x added, i.e. move right
		}
		else if (x > y && x > n-1-y)//element falls into the upper image area II
		{
			retvec[y][x] = index;
			++y;//y increment, that is, move down
		}
		else if (x <= y && x > N-1-y)//element falls into the upper image area three
		{
			retvec[y][x] = index;< c23/>--x;//x decrease, that is, move left
		}
		else if (x < y && x <= n-1-y)//element falls into the upper image area four
		{
			retvec[y][x] = in Dex;
			--y;//y decrease, that is, move up
		}
	}
	return retvec;
}


The results are as follows, note that the matrix of odd and even rows is different from the starting point (even the center point is not in the center), so the direction of the first few elements is not the same, but the overall direction is the same: Understanding the above principles, you can focus on the requirements of the topic, slightly adjust the code to form various directions, Different order of the spiral matrix, as long as the judge in a certain area, X and y should be so changed, especially to pay attention to when the point falls on the area of the line when the situation, to judge well. For example, ask to rotate counterclockwise from outside: You can convert to the same example as before, rotate clockwise from inside out, but fill the position from the largest element:
vector<vector<int>>  Generatespiralmatrixi (int n) {
	vector<vector<int>> retVec (n, Vector<int> (n, 0));

	if (0 = = N)
	{
		return retvec;
	}

	int x = N/2;
	int y = N/2;
	for (size_t index = n * N; index >= 1;--index)
	{
		...//same as Before:d	
	}
	return retvec;
}

Finally, it is important to note that you must adjust the x and Y in order of fill, for example, in the previous example, right (x + +), down (y++), left (x--), Up (y--), then the code will be written in this order, in the direction of the spiral, it is important to note this. The above is about the spiral matrix of a little finishing.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.