ZOJ 3470 Magic Squares

Source: Internet
Author: User

Magic Squares

Time Limit: 2 Seconds Memory Limit: 65536 KB


One day, the astronauts from China arrived in a planet that they have never been before. First, they sent a robot called AAA to launch e the environment as usual.

However, AAA became confused as soon as it departed from the aerostat: it found that the surface of the planet was divided into blocks we call magic squares, each block is marked
By an unique integer in a special order (as shown in the left graph ). fortunately, AAA is so clever that it soon found the relationship between these blocks and the Descartes coordinate system (as shown in the right graph ).

Before each movement, AAA wocould send its current block number to the monitor, then move to the right position the monitor order it to. Assuming that the monitor wocould only order AAA
Move to the block neighbored to the current block. We say two blocks are neighbor if they share one same edge.

To complete the task in time, the monitor need to respond to the request of the robot as soon as possible. But the computing progress may be too complicated for beings. So they now turn
To you, an excellent programmer, help them list the blocks the AAA can reach at the time in order to sent command to the robot in a short time.

Input

The first line of input is the number of test cases T, then t lines follow, each is a test case with only one positive integers n no larger than 2,000,000,000.

Output

The output consists of exactly t lines, one line for each case.
Line I (1 <= I <= T) shocould contain an increasing sequence of integers separated by single Spaces -- the numbers of the blocks that the robot cocould reach (with its current block numbered N ).

Sample Input

15

Sample output

4 6 16 18


First determine the number of layers, and then divide the number into eight cases (at four corners or on four sides) to push two adjacent numbers (because two of the four numbers are smaller than 1 and the other two numbers are different layers, which need to be discussed in detail ), finally, you can write 8 cases into 4 cases ~

The Code is as follows:

# Include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> # include <cmath> using namespace std; int save [5]; void s_save (int x, int y, int m, int n) {save [0] = x; save [1] = y; save [2] = m; save [3] = n;} int dif (int num, int I, int dig, int d) {if (num <dig) {s_save (num-d + 8-2 * I, num-1, num + 1, num + d + 2 * I); return 1 ;} else if (num = dig) {s_save (num-1, num + 1, num + d + 2 * I, Num + d + 2 * (I + 1); return 1;} return 0;} void solve (int num, int cov, int dig, int d) {int flag = 0; dig + = cov; flag = dif (num, 0, dig, d); // if (! Flag) // determine whether it is on the left side {dig + = cov * 2; flag = dif (num, 1, dig, d);} if (! Flag) // determine if it is next {dig + = cov * 2 + 1; flag = dif (num, 2, dig, d);} if (! Flag) // determine whether it is on the right side {dig + = cov * 2 + 1; flag = dif (num, 3, dig, d);} if (! Flag) // determine whether the cursor is on the top of a circle {s_save (num-d, num-1, num + 1, num + d + 8 );}} void find_cov (int num) // determine the number of layers where the element is located {int d = 3, cov = 0, dig = 1; while (1) {if (cov) d + = 8; dig + = d; if (num <= dig) break; ++ cov;} if (num = 1) s_save (2, 4, 6, 8); else solve (num, cov, dig-d, d);} int main () {# ifdef test freopen ("in.txt", "r", stdin); # endif int t, num; scanf ("% d", & t); while (t --) {scanf ("% d", & num); find_cov (num ); printf ("% d \ n", save [0], save [1], save [2], save [3]);} return 0 ;}


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.