Clockwise, counterclockwise, detailed description of the zigzag Matrix

Source: Internet
Author: User

Recently, there have been a lot of research on the matrix. As a result, some people have been discussing this issue in some QQ groups, and there are a lot of discussion participants. It seems that everyone is quite interested in this issue; secondly, I encountered such a problem during my interview with youdao last year. Unfortunately, I didn't answer the question at the time, so I felt quite sorry to miss youdao. But this is not all, because I did this question a year ago, and I have considered it for a long time. It is a company's interview question, but it cannot be written when it really needs to be used, as you can see, Confucius has learned a lot about the warmth of his learning, but he has never used it until now. It took a long time to go back to the interview and finally found the question that I had done. What makes me uncertain is that the program is in a folder called "youdao test, there is a feeling of instant petrochemical. It is really a popular saying on the Internet that "it will be paid back sooner or later", so I have to think about it and understand this matrix problem.

First, the clockwise matrix.

1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

Through our observation, we can know that the matrix increases counterclockwise along the largest circle first. When it is enclosed in a circle, and then enters the second circle, it also increases counter-clockwise. Once the rule is found, how can we output such a matrix? Of course, we need to find the law of language. First, I first use a simple and understandable method. First, we define four variables, top, bottom, left, right, which correspond to the top, bottom, left, and right directions of the matrix respectively. In the top row of the matrix, we find that the X-axis remains unchanged, but only the y-axis is changed. In the rightmost column of the Matrix, only the y-axis is changed, while the y-axis is not changed, and so on, we can get the following code.

Void clockwise ()
{
Int Top =-1, Bottom = N, Left =-1, Right = N;
Int I, J;
Int n = 1;
I = Top + 1, j = left + 1;
While (top <= bottom & left <= right)
{
Top ++; bottom --; left ++; right --;
While (j <= right)
{
A [I] [J] = n ++;
J ++;
}
J --; I ++;
While (I <= bottom)
{
A [I] [J] = n ++;
I ++;
}
I --; j --;
While (j> = left)
{
A [I] [J] = n ++;
J --;
}
J ++; I --;
While (I> top)
{
A [I] [J] = n ++;
I --;
}
I ++; j ++;
}
}

This method is simple and easy to understand, because it does not involve complicated inductive reasoning. The following method is obscure. This method is based on a deep understanding of the image. It may not be easy to understand at the beginning, however, you should be able to understand the details after reading them several times.

Void clockwise (int A [] [N])
{
Int I, j, k = 1;
For (I = 0; I <n/2; I ++)
{
For (j = I; j <n-I-1; j ++)
A [I] [J] = K ++;
For (j = I; j <n-I-1; j ++)
A [J] [n-I-1] = K ++;
For (j = n-I-1; j> I; j --)
A [N-i-1] [J] = K ++;
For (j = n-I-1; j> I; j --)
A [J] [I] = K ++;
}
If (N % 2 = 1)
A [n/2] [n/2] = K;
}
}

After understanding the increment in the clockwise direction, the counter-clockwise Matrix program can be well written, and it will not be written here, leaving readers with their own programming exercises.

Next I will focus on the generation of the zigzag matrix, which is commonly known as the "font" matrix. Those familiar with image processing should be familiar with the zigzag array. In the JPEG image algorithm, the image is first processed in blocks, which are generally divided into overlapping blocks with the same size, the Quantization result retains the coefficient of the low-frequency part and removes the coefficient of the high-frequency part. The quantified coefficient is re-organized according to the zigzag scan, and then carried out the Harman encoding.

1 2 6 7 15

3 5 8 14 16

4 9 13 17 22

10 12 18 21 23

11 19 20 24 25

Analyze the matrix carefully. First, start from the upper left corner, increment one digit to the right, and then increment along the diagonal line from the upper right to the lower left, but go down along the border, then, it increments from bottom left to top right. Images are easy to understand.

Void zigzag (int A [] [N])

{

Int I, j, min = 1, max = N * N;

For (I = 0; I <n/2; I ++)

{

If (I % 2 = 0)

{

For (j = I; j> = 0; j --)

{

A [J] [I] = min ++;

A [N-1-j] [N-1-i + J] = max --;

}

}

If (I % 2! = 0)

{

For (j = 0; I <= I; I ++)

{

A [J] [I-j] = min ++;

A [N-1-j] [N-1-i + J] = max --;

}

}

}

}

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.