hdu1998 Odd-order Magic (array fill number)

Source: Internet
Author: User

Odd-order Rubik's CubeTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3071 Accepted Submission (s): 1614


Problem description The elements of an n-order phalanx are,..., n^2, each of its rows, each column and 2 diagonal elements and equal, so
Square is called Rubik's Cube. n is an odd number when we have 1 constructs, called "top right", for example, given below n=3,5,7
Rubik's Cube.
3
8 1 6
3 5 7
4 9 2
5
17 24 1) 8 15
23 5 7) 14 16
4 6 13) 20 22
10 12 19) 21 3
11 18 25) 2 9
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The 1th line in the middle of the number is always 1, the last 1 lines in the middle of the number is n^2, his right is 2, from the three Rubik's Cube, you can see "right
Above "What it means."

Input contains multiple sets of data, first entering T, which indicates that there is a T group of data. 1 rows per set of data give N (3<=n<=19) is odd.

Output for each set of data, the N-order Rubik's Cube, 4 squares per number, right-aligned

Sample Input
235

Sample Output
   8   1   6   3   5   7   4   9   2  ,   1   8, 5 7   4 6 (   3  ) of 18   2   9

Author[email protected]
Sourceecjtu Spring Contest
Recommendlcy | We have carefully selected several similar problems for you:1994 1996 1995 1999 1997

How to say this question, when I see the first eye of this problem is definitely to find the law, and the entire array is followed.

Because you have to program it. Certainly not too complicated a rule. So it took 5 minutes to find the pattern.

Take n=5 for example, a[0][n/2]=1,a[n-1][n/2]=n*n,a[n-1][n/2+1]=2. And then you'll find 3 in the top right of 2.

5 in the top right of 4 6 is just below 5 ... Wait a minute.. We will find that if M is in a[x][y] position, then m+1 is in the a[x-1][i+1] position (and of course other conditions are satisfied),

As for other conditions that are not satisfied, that is a[x-1][i+1] there are already several, then the m+1 should be under M. And if M is in a[x][n-1], then m+1 is in a[x-1][0]. If M

In A[0][y] then m+1 in a[n-1][y+1]. If M is also below M in a[0][n-1],m+1.

Please attach the code:

#include <stdio.h> #include <string.h>int main () {    int map[20][20],ncase,n;    scanf ("%d", &ncase);    while (ncase--)    {        memset (map,0,sizeof (map));        scanf ("%d", &n);        Map[n-1][n/2]=n*n;        Map[0][n/2]=1;        int t=2;        int i=n-1,j=n/2+1;        while (T<n*n)        {            if (!map[i][j]&&i>=0&&i<n&&j<n&&j>=0)            map[i][j]=t,i--, j++,t++;            else            {                if (i<0&&j<n)                i=n-1;                else if (i>=0&&j==n)                j=0;                else                i+=2,j--;            }        }        for (int i=0;i<n;i++)        {            for (int j=0;j<n;j++)            printf ("%4d", Map[i][j]);            printf ("\ n");}    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu1998 Odd-order Magic (array fill number)

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.