Programmer interview book-Analysis of solution to spiral queue 2

Source: Internet
Author: User

Continued to write "Programmer interview book-Analysis of spiral queue solution"

First, attach the C ++ code. People who understand the Code do not have to look at the example below.

# Include <iostream> <br/> using namespace STD; <br/> # define max (A, B) (a)> (B )? (A) :( B) <br/> # define ABS (A) (a)> 0? (A):-(a) <br/> // spiral queue solution <br/> int Foo (int x, int y) {<br/> int T = max (ABS (x), ABS (y); <br/> // int u = 2 * t; <br/> // int v = (2 * T-1) * (2 * T-1); <br/> int u = T + T; <br/> int v = u-1; <br/> V = V * V + T; <br/> If (y = T) {<br/> V + = u-X; <br/>}< br/> else if (x =-T) {<br/> V + = 2 * U-y; <br/>}< br/> else if (y =-T) {<br/> V + = 3 * u + X; <br/>}< br/> else {// X = T must be placed behind y =-T <br/> V + = y; <br/>}< br/> return V; <br/>}< br/> int main () <br/>{< br/> int X, Y; <br/> for (y =-4; y <= 4; y ++) {<br/> for (x =-4; x <= 4; X ++) {<br/> printf ("% 5d", Foo (x, y); <br/>}< br/> cout <Endl; <br/>}< br/> printf ("Enter the coordinates of the numbers, separate them with the Space key, and end with the Enter key/N "); <br/> while (scanf ("% d", & X, & Y) = 2) <br/> printf ("% d/N ", foo (x, y); </P> <p> return 0; <br/>}< br/>

Example:

Assume that the coordinate is the value of a number (1,-1.

Conclusion: we can see from the figure that the number in the coordinate (1,-1) is 9, and it is in the circle 1.

Next I will follow the steps in the previous article "Programmer interview book-spiral queue Solution Analysis.

1. Determine the lap number t.

T = max (| x |, | Y | );

Here | x | = 1, | Y | = 1, so t = max (1, 1) = 1.

2. determine the maximum value of the ring t-1.

Max (t-1) = (2 t-1) 2
.

Here max (t-1) = max (0) = 1.

3. determine the value of the number v.

The value of the number V = the maximum value of the previous circle + the clockwise step.

The maximum value of the previous circle has been obtained in step 1, which is 1.

The clockwise step is calculated according to the classification standard below.

Side length u = 2 t = 2. 2 ~ 9 is a square with a side length of 2.

Coordinates (1,-1) are first sorted by four classifications and do not meet the conditions for the first two classifications (y = T = 1, x =-T =-1 ), but it satisfies both the last two conditions (y =-T =-1, x = T = 1 ). Since the coordinate first satisfies Condition 3, it is calculated according to the formula of Condition 3:

Clockwise step = 3u + U/2 + x = 3*2 + 2/2 + 1 = 8

V = maximum value of the previous circle + clockwise step = 1 + 8 = 9.

Complete.

 

Here, I will explain the clockwise step-by-step formula. Why?

The so-called clockwise step refers to the number of steps that need to be taken to reach the desired coordinates starting from the maximum position in the previous circle and walking along the current circle clockwise.

In this example, start from the position 1 and walk through 2, 3, 4, 5,... clockwise ,......, 9. Each time a number passes through a step, 8 steps are taken.

The number that arrives at the edge y =-T must walk through the side X = T, y = t, x =-T three in sequence. Therefore, the clockwise step contains the 3u value.

U/2 makes the step size reach the intersection of Y =-T and Y axis. At this time, the clockwise step size is only related to the X coordinate. If X is 1, The step size is increased by 1, if X is-1, the step size is-1.

The other three clockwise step formulas and so on.

 

 

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.