Problem B
Magic Cube
Input: standard input
Output: standard output
Time Limit: 1 second

 

An N ´ N ´ N magic cube is a cube composed of N3 consecutive integers (usually 1 through N3) arranged in such way that each row, each column, each main diagonal of the square cross sections, and each of the four triagonals containing N integers adds up to the same sum. Though it is not easy to construct a magic cube when N is even, there is a very simple approach for constructing one when N is odd.

Fig. 1: Triagonals (dotted lines)

 

To construct an odd order magic cube, start by placing the integer 1 at the center of the bottom layer of the cube (Fig. 2). Then proceed diagonally down to the right along the central vertical square cross section (the shaded vertical plane in Fig. 2), and keep placing consecutive integers in every empty location you visit. When you run off the bottom of the plane, your reenter at the top. When you run off the right-hand side, you reenter on the left-hand side.

 

Every time you find a non-empty location on your way, just go back one layer and down one, as shown in Fig. 2 for the move from 5 to 6. Continue to proceed diagonally in the new plane, repeating the “one back and one down” step as required. If the “one back and one down” step does not work, i.e., you still arrive at a non-empty location, just go up one layer and continue moving diagonally. The “up one” step is shown in Fig. 3 for the move from 25 to 26.

Fig. 2: The “one back and one down” move

Fig. 3: The “up one” move

 

Input

The input may contain multiple test cases. Each line of the input contains an odd integer N (1 £ N £ 25) giving the dimension of the cube.

Input is terminated by a zero. This zero should not be processed.

             

Output

For each test case in the input output the corresponding magic cube as follows. First print the numbers in the top layer of the cube, followed by numbers in the second layer, then the numbers in the third layer, and so on. For each layer first print the numbers in row A (see Fig. 2 and Fig. 3) on one line from left to right (A1 to AN), followed by the numbers in row B on the next line, then row C on the third line, and so on. Print each number right aligned on a field of width 5, and separate two successive such fields on the same line by a white space character. Print a blank line between two successive layers.

 

Separate the output for every two successive test cases by a blank line.

 

 

 

 

 

 

 

 

 

 

 

Sample Input                           Output for Sample Input

3

5

0

   21     5    16

   13    27     2

    8    10    24

 

   17    19     6

    3    14    25

   22     9    11

 

    4    18    20

   26     1    15

   12    23     7

 

  110    12    44    71    78

   98   105     7    39    66

   61    93   125     2    34

   29    56    88   120    22

   17    49    51    83   115

 

   79   106    13    45    72

   67    99   101     8    40

   35    62    94   121     3

   23    30    57    89   116

  111    18    50    52    84

 

   73    80   107    14    41

   36    68   100   102     9

    4    31    63    95   122

  117    24    26    58    90

   85   112    19    46    53

 

   42    74    76   108    15

   10    37    69    96   103

  123     5    32    64    91

   86   118    25    27    59

   54    81   113    20    47

 

   11    43    75    77   109

  104     6    38    70    97

   92   124     1    33    65

   60    87   119    21    28

   48    55    82   114    16

 

 


Problem setter: Rezaul Alam Chowdhury, University of Texas at Austin