My own opinions on the spiral Matrix Algorithm

Source: Internet
Author: User

I have been very interested in some algorithms. Today I will share with you the algorithm of the spiral matrix.

After reading a lot of other people's methods, I summarized them myself and modified some of the bad ones through my own ideas.

So it's just for everyone to communicate with each other. However, due to their lack of learning skills, I hope you will give me some advice ~



Enter the subject

Below is a structure chart I have drawn.

First, I will explain this graph. I divide it into four blocks and assign values one by one. The square in the middle means that if the MatrixBase Number (that is, the number of rectangles in the spiral matrix. If it is an odd number, remove a number in the middle)If an odd number exists in the middle, the even number does not exist. You can draw the picture on the drawing. Another point is that the first block is the element assignment of the whole line, and it is responsible for the entire line from start to end, while other blocks are counted from the first to the second (For example, if the base number is 3, the element to be assigned a value in the first line is 1, 2, 3, and the second block is 4, the third block is 5, 6, and so on.)

Block 1: When values are assigned, the number of rows remains unchanged and the number of columns is changing.

Block 2: The number of columns remains unchanged, and the number of rows is changing.

Block 3: Same as Block 1

Block 4: Same as Block 2


The entire process is clear. The next step is code implementation.

# Include <stdio. h> # define n 20int main (void) {int I, J, K, base = 1, n; int a [n] [N] = {0 }; printf ("Enter the level matrix you want to obtain: \ n"); scanf ("% d", & N); For (k = 0; k <n/2; k ++) // n/2 is the number of rectangles in the matrix {for (I = K; I <= n-1-k; I ++) // Block A [k] [I] = base ++; For (j = k + 1; j <n-1-k; j ++) // Block 2 A [J] [n-1-k] = base ++; for (I = n-1-k; I> K; I --) // block 3 A [n-1-k] [I] = base ++; For (j = n-1-k; j> K; j --) // Block A [J] [k] = base ++; If (N % 2 = 1) A [(n-1)/2] [(n-1) /2] = base; // assign a value to the center of a matrix with an odd base.} // output matrix for (I = 0; I <n; I ++) {for (j = 0; j <n; j ++) printf ("%-4D", a [I] [J]); printf ("\ n");} return 0 ;}

The result output is as follows:

Matrix with an odd base number

The base number is an even matrix.

In my personal summary, there may be some omissions. Please give me some advice.

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.