一个魔方,就是一个由1到n^2的整数构成的n*n的矩阵,其中每行、每列以及两个主对角线上的数字之和都相等。
初见这个定义,最难的地方便是如何控制每行每列的数字和相等。当n为奇数时,把1放入第一行最中间的方格中。向左上方移动,并按照数字的递增顺序,把数字填入空方格。如果移出了方格(即超过了魔方的边界),则进入魔方对边的对应方格。如果一个方格已经被填入数字,则向下继续填写方格。图中给出了当n=7时魔方。
28 | 19 | 10 | 1 | 48 | 39 | 30 |
29 | 27 | 18 | 9 | 7 | 47 | 38 |
37 | 35 | 26 | 17 | 8 | 6 | 46 |
45 | 36 | 34 | 25 | 16 | 14 | 5 |
4 | 44 | 42 | 33 | 24 | 15 | 13 |
12 | 3 | 43 | 41 | 32 | 23 | 14 |
20 | 11 | 2 | 42 | 40 | 31 | 22 |
根据H.Coxeter法则,还有以下问题待解决:
1.如何控制输入数字向左上方移动;
2.如何控制在溢出方格时进入魔方对边的对应方格;
3.如何控制当已经有数字时向下填写方格。
其实这些问题并不难解决。只需要控制向左上循环输入时进行条件判定即可。
代码如下,在vs2013上编译运行通过。
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 15
int main(void)
{
static int square[MAX_SIZE][MAX_SIZE];
int i, j, row, column;
int count;
int size;
printf("Enter the size of the square: ");
scanf("%d", &size);
if (size<1 || size>MAX_SIZE + 1)
{
fprintf(stderr, "Error! Size is out of range\n");
exit(1);
}
if (!(size % 2))
{
fprintf(stderr, "Error! Size is even\n");
exit(1);
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
square[i][j] = 0;
}
}
square[0][size / 2] = 1;
i = 0;
j = (size / 2);
for (count = 2; count <= size*size; count++)
{
row = (i - 1 < 0) ? (size - 1) : (i - 1);
column = (j - 1 < 0) ? (size - 1) : (j - 1);
if (square[row][column])
i = (++i) % size;
else
{
i = row;
j = (j - 1 < 0) ? (size - 1) : --j;
}
square[i][j] = count;
}
printf("Magic Square of size %d:\n\n", size);
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
printf("%5d", square[i][j]);
printf("\n");
}
printf("\n\n");
system("pause");
return 0;
}
也可以使用用户自定义函数编写,这样可以增加可读性,但是其时间复杂性会增加。
使用自定义函数改写如下
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 15
int input();
void initialization(int, int(*)[MAX_SIZE]);
void function(int, int(*)[MAX_SIZE]);
void output(int, int(*)[MAX_SIZE]);
int main(void)
{
static int square[MAX_SIZE][MAX_SIZE];
int size;
size = input();
initialization(size, square);
function(size, square);
output(size, square);
system("pause");
return 0;
}
int input()
{
int i, j, row, column;
int count;
int size;
printf("Enter the size of the square: ");
scanf("%d", &size);
if (size<1 || size>MAX_SIZE + 1)
{
fprintf(stderr, "Error! Size is out of range\n");
exit(1);
}
if (!(size % 2))
{
fprintf(stderr, "Error! Size is even\n");
exit(1);
}
return size;
}
void initialization(int size,int (*square)[MAX_SIZE])
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
square[i][j] = 0;
}
}
}
void function(int size, int(*square)[MAX_SIZE])
{
int i, j;
int column, row;
int count;
square[0][size / 2] = 1;
i = 0;
j = (size / 2);
for (count = 2; count <= size*size; count++)
{
row = (i - 1 < 0) ? (size - 1) : (i - 1);
column = (j - 1 < 0) ? (size - 1) : (j - 1);
if (square[row][column])
i = (++i) % size;
else
{
i = row;
j = (j - 1 < 0) ? (size - 1) : --j;
}
square[i][j] = count;
}
}
void output(int size, int(*square)[MAX_SIZE])
{
int i,j;
printf("Magic Square of size %d:\n\n", size);
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
printf("%5d", square[i][j]);
printf("\n");
}
printf("\n\n");
}
这样整个程序的可读性会提升不少,读起来会更轻松。
附运行截图一张
参考: Fundamentals of Data Structures in C__Ellis Horowitz