# Spiral Matrix Algorithm

Source: Internet
Author: User

I remember the question my teacher gave me when I was a sophomore. It was not very difficult to go back and think about it for a long time. Today, I found some information on my computer. I checked it out. I found a lot of questions on the Internet, but it seems much more complicated on the Internet (maybe my personal feeling, i'm an alternative. The time complexity is basically O (N ^ 2.

There are several categories of this spiral matrix. The question I got was to rotate clockwise out of the center, as shown below:

7 8 9
6 1 2
5 4 3

This is too small to analyze the rule, so it's big:

Do you see the rule? Find a draft paper for research.

I thought so:

The red line equation y = x, and the green line equation y =-x + 4 4 indicates the rectangular side length.

The two straight lines divide the region into four parts and divide the boundary values of each region. There are four types of coordinate variation rules for each region. x ++, y ++, x --, y --, next, we can analyze the algorithm carefully:

`Void SpiralArray (int size, int ** ar) {int a = size/2*2 + 1; // ensure that the side length is odd int y = a/2, x = a/2; // for (int I = 1; I <= size * size; I ++) // (int I = size * size; I >=1; I --) {if (x <= a-y-1 & x> = y) {ar [y] [x] = I; x ++ ;} else if (x> a-y-1 & x> y) {ar [y] [x] = I; y ++ ;} else if (x> a-y-1 & x <= y) {ar [y] [x] = I; x --;} else if (x <= a-y-1 & x <y) {ar [y] [x] = I; y --;}}}`

The complete code is as follows:

`# Include <iostream> # include <time. h> using namespace std; void SpiralArray (int size, int ** ar) {int a = size/2*2 + 1; // ensure that the side length is odd int y = a/2, x = a/2; // start from the center point for (int I = 1; I <= size * size; I ++) // (int I = size * size; I> = 1; I --) {if (x <= a-y-1 & x> = y) {ar [y] [x] = I; x ++;} else if (x> a-y-1 & x> y) {ar [y] [x] = I; y ++;} else if (x> a-y-1 & x <= y) {ar [y] [x] = I; x --;} else if (x <= a-y-1 & x <y) {ar [y] [x] = I; y -- ;}} int main () {int n; d O {cout <"Please input a number (\" 0 \ "is exit):"; cin> n; if (n = 0) break; int a = n/2*2 + 1; // ensure that the edge length is an odd int ** g = new int * [a]; for (int I = 0; I <; I ++) g [I] = new int [a]; for (int p = 0; p <a; p ++) for (int q = 0; q <a; q ++) g [p] [q] = 0; clock_t start, finish; start = clock (); SpiralArray (n, g ); finish = clock (); double duration = (double) (finish-start)/CLOCKS_PER_SEC; printf ("% f seconds \ n", duration); char c; cout <"Output? (Y/n): "; cin> c; if ('n' = c) continue; int ws = 2; if (n x n/100> = 1) ws = 4; else if (n * n/10> = 1) ws = 3; for (int y = 0; y <a; y ++) {for (int x = 0; x <a; x ++) {if (g [y] [x] = 0) {cout. width (ws); cout <'';} else {cout. width (ws); cout <g [y] [x] ;}} cout <endl ;}for (I = 0; I <a; I ++) {delete [a] g [I]; g [I] = NULL;} delete [a] g; g = NULL;} while (1); return 0 ;}`

There is also a kind of spiral matrix from the outside to the inside, such:

You only need to make a few changes. The analysis is as follows:

The order of values and the principle of the algorithm remain unchanged. The value is assigned from a large number. Therefore, the for loop is changed to: for (int I = size * size; I> = 1; I --), the result is:

The axis of symmetry is x = a-x-1, just bring in. In addition, this matrix does not have the same parity. The complete code is as follows:

`# Include <iostream> # include <time. h> using namespace STD; void spiralarray (INT size, int ** AR) {int A = size; int y = A/2, x = A/2; // For (INT I = size * size; I> = 1; I --) // (INT I = 1; I <= size * size; I ++) {If (x <= a-y-1 & x> = y) {ar [y] [a-x-1] = I; X ++ ;} else if (x> a-y-1 & x> Y) {ar [y] [a-x-1] = I; y ++ ;} else if (x> a-y-1 & x <= y) {ar [y] [a-x-1] = I; X --;} else if (x <= a-y-1 & x <Y) {ar [y] [a-x-1] = I; y -- ;}} int main () {int N; Do {cout <"Please input a number (\" 0 \ "is exit):"; CIN> N; If (n = 0) break; int A = N; int ** G = new int * [a]; for (INT I = 0; I <A; I ++) G [I] = new int [a]; for (INT p = 0; P <A; P ++) for (INT q = 0; q <; Q ++) g [p] [Q] = 0; clock_t start, finish; Start = clock (); spiralarray (n, g); finish = clock (); double duration = (double) (finish-Start)/clocks_per_sec; printf ("% F seconds \ n", duration); char C; cout <"output? (Y/N): "; CIN> C; If ('n' = c) continue; int Ws = 2; If (N x n/100> = 1) ws = 4; else if (N * n/10> = 1) Ws = 3; for (INT y = 0; y <A; y ++) {for (INT x = 0; x <A; X ++) {If (G [y] [x] = 0) {cout. width (WS); cout <'';} else {cout. width (WS); cout <G [y] [x] ;}} cout <Endl ;}for (I = 0; I <A; I ++) {Delete [a] G [I]; G [I] = NULL;} Delete [a] G; G = NULL;} while (1); Return 0 ;}`
Related Keywords:

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.