C language to achieve the magic square algorithm of magic square magic cube single even rubik's cube
- 2020-04-02 01:59:25
- OfStack
For example, the third order magic square matrix is:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131126095338.jpg? 2013102695352 ">
What is the pattern of the magic square?
Magic squares are divided into fantasy squares and fantasy squares. The magic square is further divided into multiples of 4 (e.g., 4, 8, 12...). The sum is not a multiple of 4 (e.g. 6,10,14...). Two kinds. The following are described separately.
2 magic cube algorithm
2.1 rules and algorithms of magic cube
Magic cube (n = 2 * m + 1, m =1, 2, 3...) The pattern is as follows:
The number 1 is in the middle column of the first row in the square matrix;
The number a of 1
<
a.
Or less n2) the number of rows is 1 less than the number of rows of a-1. If the number of rows of a-1 is 1, the number of rows of a is n;
The number a of 1
<
a.
Or less n2) the number of columns is 1 greater than the number of a-1 columns. If the number of columns of a-1 is n, the number of columns of a is 1.
If a minus 1 is a multiple of n, then a of 1
<
a.
Or less n2) has 1 more rows than a-1, and the same number of columns as a-1.
2.2 implementation of magic cube algorithm in C language
#include <stdio.h>
// Author: http://furzoom.com/
//N is the order of the rubik's cube
#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 rule of the even rubik's cube is as follows:
From small to large, that is, 1,2,3... N2 order to fill the magic square from left to right, from top to bottom;
Divide the magic square into several 4 x 4 subsquares and take out the diagonal elements of the subsquares.
Fill the extracted elements into the vacancy of the n by n square matrix in descending order.
C language implementation
#include <stdio.h>
// Author: http://furzoom.com/
//N is the order of the rubik's cube
#define N 12
int main()
{
int a[N][N];//Store the rubik's cube
int temparray[N*N/2];//Store the retrieved elements
int i;//Loop variables
int col, row;//Col column, row row
//Initialize the
i = 1;
for(row = 0;row < N; row++)
{
for(col = 0;col < N; col ++)
{
a[row][col] = i;
i++;
}
}
//Take the diagonal elements of the submatrix and arrange them in exactly the order from smallest to largest
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++;
}
}
}
//Fill the extracted elements into the n by n matrix in descending order
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 square
for(row = 0;row < N; row++)
{
for(col = 0;col < N; col ++)
{
printf("%5d",a[row][col]);
}
printf("n");
}
return 0;
}
3.2 n = 4 * m + 2 (m = 1,2,3...) Rubik's cube (single even rubik's cube)
algorithm
Let's say that k is equal to 2 times m plus 1; A single - even rubik's cube is one of the more complex of the rubik's cubes.
Divide the rubik's cube into A, B, C and D square matrices of order k, as shown in the following figure. Fill in A, D, B and C in turn by using the method mentioned above.
Swap the A and C elements of the rubik's cube. For the middle row of the rubik's cube, swap the corresponding m elements from the middle column to the right. For the other rows, swap the m columns from left to right.
Swap the B and D elements, swap
#include <stdio.h>
// Author: http://furzoom.com/
//N is the order of the rubik's cube
#define N 10
int main()
{
int a[N][N] = { {0} };//Store the rubik's cube
int i,k,temp;
int col,row;//Col column, row row
//Initialize the
k = N / 2;
col = (k-1)/2;
row = 0;
a[row][col] = 1;
//To generate magic cube A
for(i = 2; i <= k*k; i++)
{
if((i-1)%k == 0 )//The previous one 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 and D cubes 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)//The middle row, swap the m column from the middle column to the right, N is equal to 2 times 2m plus 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//The other rows, swap the m columns from left to right,N is equal to 2 times 2m plus 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 the middle column to the left m minus 1 column, N is equal to 2 times 2m plus 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 matrix
for(row = 0;row < N; row++)
{
for(col = 0;col < N; col ++)
{
printf("%5d",a[row][col]);
}
printf("n");
}
return 0;
}