NOIP2015 The Magic Square

Question A: The Magic Square

Time Limit:  1 Sec   Memory Limit:  128 MB
Commits:  163   Resolved:  126
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]

Topic description

A magic square is a magical N*N matrix: it consists of the numbers 1, 2, 3, ..., N*N, and the sum of the numbers in each row, column and two diagonals is the same.
When N is odd, we can construct a magic square by
first writing 1 in the middle of the first row.
After that, fill in each number K(K=2,3,…,N*N) in order from small to large as follows:
1. If (K−1) is in the first row but not in the last column, then fill in K in The last row, the right column of the column where (K-1) is located;
2. If (K-1) is in the last column but not in the first row, then fill in K in the first column, the row above the row where (K-1) is located ;
3. If (K−1) is in the first row and last column, then fill in K just below (K−1);
4. If (K−1) is neither in the first row nor in the last column, if If the upper right of (K−1) has not been filled, then fill in K at the upper right of (K−1), otherwise, fill in K directly below (K−1).
Now given N, please construct the magic square of N*N according to the above method.

enter

The input is only one line, containing an integer N, the size of the magic square. 1≤N≤39 and N is odd

output

The output contains N lines, each line contains N integers, that is, the magic square of N*N constructed by the above method. Separate two adjacent integers with a single space.

sample input

3

Sample output

8 1 6

3 5 7

4 9 2


#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int a[45][45];

intmain()
{
    int n;
    while(cin >> n)
    {
        struct Num
        {
            int value;
            int hang;
            int lie;
        }k;
        memset(a, 0, sizeof(a));
        k.value = 1;
        a[1][(n+1)/2] = k.value;
        k.hang = 1;
        k.lie = (n+1)/2;
        while(k.value < n*n)
        {
            //printf("%d\t%d\t%d\n", k.hang, k.lie, k.value);
            if(k.hang == 1 && k.lie != n)//1.
            {
                a[n][++k.lie] = ++k.value;
                k.hang = n;
            }
            else if(k.lie == n && k.hang != 1)//2.
            {
                a[--k.hang][1] = ++k.value;
                k.lie = 1;
            }
            else if(k.hang == 1 && k.lie == n)//3.
                a[++k.hang][k.lie] = ++k.value;
            else if(k.hang != 1 && k.lie != n )//4.
                if(!a[k.hang-1][k.lie+1])
                    a[--k.hang][++k.lie] = ++k.value;
                else
                    a[++k.hang][k.lie] = ++k.value;
            //printf("%d\t%d\t%d\n\n", k.hang, k.lie, k.value);
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(j==n) cout << a[i][j] << endl;
                else cout << a[i][j] << " ";
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325844575&siteId=291194637