魔方 – 使用 C 语言和 Python 例子
什么是魔方?
魔方是一种具有特殊数字排列的方阵。这些数字的排列方式使得每条对角线、行和列上的数字之和保持不变。魔方游戏是用于娱乐数学的简单逻辑谜题。
魔方示例:
上图是 3 阶魔方的示例。每条对角线、行和列的总和为 15。
魔方阵的工作原理
魔方是由 n^2 个正整数组成的 n*n 矩阵。方阵的行数或列数称为矩阵的阶。
通常,魔方谜题的阶数为奇数,取值范围为 1 到 n^2。每条对角线、行和列的和都相同。这个数字称为魔方和或魔方常数。通常,这个魔方和取决于矩阵的阶数。n 阶魔方和的魔方公式为:
让我们以整数 3 的魔方为例。因此,魔方和将是-
为什么他们被称为魔术师?
古代数学家对几种有趣的数字组合的性质非常着迷。幻方就是其中之一。幻方的最早证据可以追溯到公元前 190 年的中国。
一些研究表明,在古代日本、印度和阿拉伯,存在着魔方拼图的证据。根据一些传说,人们认为这些特殊的数字组合与魔法世界有关。因此,这些方块被称为魔方。
魔方类型
魔方数学有不同的变体——
- 普通魔方: 这种类型的魔方包含前 n^2 个数字。
- 半魔方: 在这种类型中,只有行和列的总和等于魔法常数。
- 简单魔方: 与之前的类型不同,行、列和两个对角线的总和等于魔法常数。
- 最完美魔方: 这是一个具有两个特殊性质的普通魔方。这里,矩阵中每个 2 x 2 子方格的总和为 2(n^2+1)。任何距离网格 n/2 个方格的数字对的总和为 n^2+1。
根据性质,魔方的种类有很多。但每当我们提到魔方时,我们假设它是一个奇数阶正则简单魔方。
生成魔方的算法
生成奇数阶魔方的算法是:
- 第一个数字或 1 将存储在 (n/2, n-1) 处,其中第一个坐标是行位置,第二个坐标是列位置。对于后续步骤,我们将此位置表示为 (x, y)。
- 下一个数字将存储在 (x-1, y+1)。如果位置无效,那么我们将考虑以下情况。
- 如果行位置为 -1,则会扭曲为 n-1。同样,如果计算出的列位置为 n,则会扭曲为 0。
- 如果计算的位置已经包含数字,则行位置将增加 1,列位置将减少 2。
- 如果行位置为 -1 且对应的列位置为 n,则新位置将为 (0, n-2。
请注意: 该算法仅生成奇数阶的有效魔方。我们还认为该魔方是具有前 n^2 个数字的普通魔方。此外,对于相同的 n 值,可以有多个解。
让我们举个例子,看看它是如何工作的。假设我们想找到 3 阶魔方。由于它是一个简单而正常的奇数阶魔方,它将包含从 1 到 3^2 或 9 的所有数字。
如何运作的?
据我们说 算法,步骤如下:
步骤1) 第一个数字或 1 将位于 (3/2, 3-1) 或 (1, 2) 位置。按照惯例,在后续步骤中,请考虑 x= 1 和 y= 2。
步骤2) 其余数字的位置将按以下方式计算-
2号的位置:
下一个数字将位于 (x-1, y+1) 或 (0, 3),这不是有效位置。根据条件 (a),相应的有效位置将是 (0,0)。因此,x= 0,y= 0。
3号的位置:
数字 3 将位于 (x-1, y+1) 或 (-1, 1),这不是有效位置。通过使用条件 (a),有效行位置将是 n-1 或 2。因此,数字 3 将位于 (2, 1)。对于下一个数字,x= 2,y= 1。
4号的位置:
数字 4 应该位于 (x-1, y+1) 或 (1, 2),这是一个有效位置。但该位置已经包含一个数字。根据条件 (b),有效位置将是 (1+1, 2-2) 或 (2,0)。对于下一个数字 x= 2,y= 0。
5号的位置:
数字 5 应位于 (x-1, y+1) 或 (1, 1),这是一个有效位置。对于下一个数字 x= 1,y= 1。
6号的位置:
数字 6 应位于 (x-1, y+1) 或 (0, 2),这是一个有效位置。对于下一个数字 x= 0,y= 2。
7号的位置:
数字 7 应该位于 (x-1, y+1) 或 (-1, 3),这不是有效位置。根据条件 (c),有效位置应为 (0, n-2) 或 (0, 1)。对于下一个数字 x= 0,y= 1。
8号的位置:
数字 8 应位于 (x-1, y+1) 或 (-1, 2),这不是有效位置。使用条件 (a),有效位置应为 (2, 2)。对于下一个数字 x= 2,y= 2。
9号的位置:
数字 9 应该位于 (x-1, y+1) 或 (1, 3),这不是有效位置。根据条件 (a),有效位置应为 (1, 0)。
伪代码
Begin Declare an array of size n*n Initialize the array to 0 Set row = n/2 Set column = n-1 For all number i: from 1 to n*n If the row = -1 and column = n row = 0 column = n-2 Else If row = -1 row = n-1 If column = n column = 0 If the position already contains a number decrement column by 2 increment row by 1 continue until the position is not 0 Else put the number i into the calculated position increment i Increment column value Decrement row value End
C++ 密码魔方
输入:
/* A C/C++ program for generating odd order magic squares */ #include <bits/stdc++.h> using namespace std; void GenerateMagicSquare(int n) { int magic[n][n]; //initializing the array for(int i=0; i<n; i++) for(int j=0; j<n; j++) magic[i][j] = 0; //setting row and column value int i = n / 2; int j = n - 1; for (int k = 1; k <= n * n;) { //checking condition (c) if (i == -1 && j == n) { j = n - 2; i = 0; } else { //checking condition (a) if (j == n) j = 0; if (i < 0) i = n - 1; } //checking condition (b) if (magic[i][j]) { j -= 2; i++; continue; } else { //placing the number into the array magic[i][j] = k; k++; } //for the next number setting (i-1, j+1) j++; i--; } //printing the matrix for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << magic[i][j] << " "; cout << endl; } } int main() { //This code works for only odd numbers int n = 7; cout<<"The magic sum is " << n*(n*n+1)/2 <<endl; GenerateMagicSquare(n); return 0; }
示例输出:
The magic sum is 175 20 12 4 45 37 29 28 11 3 44 36 35 27 19 2 43 42 34 26 18 10 49 41 33 25 17 9 1 40 32 24 16 8 7 48 31 23 15 14 6 47 39 22 21 13 5 46 38 30
Python 密码魔方
def GenerateMagicSquare(n): #initializing the array magic = [[0 for x in range(n)] for y in range(n)] #setting row and column value i = n // 2 j = n - 1 k = 1 while k <= (n * n): #checking condition (c) if i == -1 and j == n: j = n - 2 i = 0 else: #checking condition (a) if j == n: j = 0 if i < 0: i = n - 1 #checking conditon (b) if magic[i][j]: j = j - 2 i = i + 1 continue else: #placing the number into the array magic[i][j] = k k = k + 1 #for the next number setting (i-1, j+1) j = j + 1 i = i - 1 #printing the matrix for i in range(0, n): for j in range(0, n): print('%2d ' % (magic[i][j]),end='') if j == n - 1: print() #This code works for only odd numbers n = 7 print("The magic sum is ",n * (n * n + 1) // 2, "\n") GenerateMagicSquare(n)
示例输出:
The magic sum is 175 20 12 4 45 37 29 28 11 3 44 36 35 27 19 2 43 42 34 26 18 10 49 41 33 25 17 9 1 40 32 24 16 8 7 48 31 23 15 14 6 47 39 22 21 13 5 46 38 30
复杂度分析
- 空间复杂度: 为了维护魔方矩阵,我们需要一个 n*n 的数组。因此,空间复杂度为 O(n^2)。
- 时间复杂度: 我们用来生成魔方数学的代码由两个循环组成。外循环运行 n 次,内循环也运行 n 次。最终时间复杂度为 O(n^2)。