For example, the third-order demon Phalanx is:

**What is the rule of the demon Phalanx?**

The Magic square is divided into magical square and even magic square. And even Magic square is divided into 4 multiples (such as 4,8,12 ...). And not multiples of 4 (e.g. 6,10,14 ...). ) Two kinds. The following are described separately.

**2 Odd Rubik's Cube algorithm**

The rule and algorithm of 2.1 odd Rubik's Cube

**Odd Rubik's Cube (Order n = 2 * m + 1,m =1,2,3 ...) ) The following laws:**

The number 1 is located in the middle of the first row of the square array;

Number A (1 < a≤n2) Number of rows is 1 less than the number of rows, if the A-1 row number is 1, then A's row number is n;

Number A (1 < A≤N2) is larger than the number of A-1 columns 1, if the A-1 column number is n, then a column number 1;

If A-1 is a multiple of n, the number of rows a (1 < A≤N2) is 1 greater than the number of A-1 rows, and the number of columns is the same as A-1.

**The realization of C language of 2.2 odd Rubik's Cube algorithm**

__Copy Code__ code as follows:

#include <stdio.h>

author:http://furzoom.com/

n is the cube order

#define N 11

int main ()

{

int a[n][n];

int i;

int col,row;

Col = (N-1)/2;

row = 0;

A[row][col] = 1;

for (i = 2; I <= n*n; i++)

{

if ((i-1)%N = = 0)

{

row++;

}

Else

{

If row = 0, then row = N-1, or row = Row-1

row--;

row = (row+n)%N;

If col = N, then col = 0, or col = col + 1

Col + +;

Col%= N;

}

A[row][col] = i;

}

for (row = 0;row<n;row++)

{

for (col = 0;col < N; col + +)

{

printf ("%6d", A[row][col]);

}

printf ("\ n");

}

return 0;

}

**Algorithm 2: Order n = 4 * m (M =1,2,3 ...) ) The laws of the Rubik's Cube are as follows:**

By numbers from small to large, that is, 1,2,3......n2 order to the magic square from left to right, from top to bottom to fill;

Dividing the magic square into several 4x4 matrices, and removing the elements on the diagonal of the Sub square;

The removed elements are then filled in the order from large to small to the vacant place in the NxN square.

C Language Implementation

__Copy Code__ code as follows:

#include <stdio.h>

author:http://furzoom.com/

n is the cube order

#define N 12

int main ()

{

int a[n][n];//storage Rubik's Cube

int temparray[n*n/2];//storage of removed elements

int i;//loop variable

int col, row;//col column, row row

Class

i = 1;

for (row = 0;row < N; row++)

{

for (col = 0;col < N; col + +)

{

A[row][col] = i;

i++;

}

}

Remove the diagonal elements from the matrix and discharge them in the order of small to large

i = 0;

for (row = 0;row < N; row++)

{

for (col = 0;col < N; col + +)

{

if ((col% 4 = row% 4) | | (3 = = (col% 4 + row% 4))

{

Temparray[i] = A[row][col];

i++;

}

}

}

To populate the NxN square with the removed elements in order from large to small

i = N*N/2-1;

for (row = 0;row < N; row++)

{

for (col = 0;col < N; col + +)

{

if ((col% 4 = row% 4) | | (3 = = (col% 4 + row% 4))

{

A[row][col] = Temparray[i];

i--;

}

}

}

Output Phalanx

for (row = 0;row < N; row++)

{

for (col = 0;col < N; col + +)

{

printf ("%5d", A[row][col]);

}

printf ("\ n");

}

return 0;

}

**3.2 Order n = 4 * m + 2 (M =1,2,3 ...) ) Rubik's Cube (single magic Cube)**

Algorithm

Set k = 2 * m + 1; The Rubik's Cube is a more complicated one in the Rubik's Cube.

The Rubik's Cube is divided into a, B, C, D four K-order matrices, the following figure of the four square matrices are singular phalanx, using the method mentioned above in turn A, D, B, c filled as a magic cube.

Exchange A, C Rubik's Cube elements, the middle line of the Rubik's Cube, Exchange from the middle column to the right of the M column corresponding elements; for other rows, swap the corresponding elements from left to right m columns.

Exchange B, D cube elements, Exchange

__Copy Code__ code as follows:

#include <stdio.h>

author:http://furzoom.com/

n is the cube order

#define N 10

int main ()

{

int A[n][n] = {{0}};//storage Rubik's Cube

int i,k,temp;

int col,row;//col column, row row

Class

K = N/2;

Col = (k-1)/2;

row = 0;

A[row][col] = 1;

Create Magic Cube A

for (i = 2; I <= k*k; i++)

{

if ((i-1)%k = = 0)//The previous number is a multiple of 3

{

row++;

}

Else

{

If row = 0, then row = N-1, or row = Row-1

row--;

row = (row+k)%k;

If col = N, then col = 0, or col = col + 1

Col + +;

Col%= K;

}

A[row][col] = i;

}

Generate B, C, D Rubik's Cube according to a

for (row = 0;row < K; row++)

{

for (col = 0;col < K; col + +)

{

A[ROW+K][COL+K] = A[row][col] + k*k;

A[ROW][COL+K] = A[row][col] + 2*k*k;

A[row+k][col] = A[row][col] + 3*k*k;

}

}

Swap A and C

for (row = 0;row < k;row++)

{

if (row = = K/2)//middle row, swap from middle column to right m column, N = 2* (2m+1)

{

for (col = k/2; col < k-1; col++)

{

temp = A[row][col];

A[row][col] = A[row + K][col];

A[row + K][col] = temp;

}

}

else//other rows, switching from left to right m column, N = 2* (2m+1)

{

for (col = 0;col < k/2;col++)

{

temp = A[row][col];

A[row][col] = A[row + K][col];

A[row + K][col] = temp;

}

}

}

Swap B and D

for (row = 0; row < k;row++)//Swap middle column left m-1 column, N = 2* (2m+1)

{

for (i = 0;i < (k-1)/2-1;i++)

{

temp = a[row][k+ K/2-i];

a[row][k+ K/2-I] = A[row + K][K+K/2-i];

A[row + K][K+K/2-i] = temp;

}

}

Output Magic Square

for (row = 0;row < N; row++)

{

for (col = 0;col < N; col + +)

{

printf ("%5d", A[row][col]);

}

printf ("\ n");

}

return 0;

}