Coxeter 方法构造魔方
给定一个奇数N ,任务是找到N阶的幻方。
例子:
Input: N = 3
Output:
6 1 8
7 5 3
2 9 4
Input: N = 5
Output:
15 8 1 24 17
16 14 7 5 23
22 20 13 6 4
3 21 19 12 10
9 2 25 18 11
方法将值 1 放在第一行的中间。设位置为 (i, j)。
- 现在向上移动一个单元格并向左移动一个单元格。在向上或向左移动时,如果我们超出了正方形的边界,则考虑在正方形的另一侧有一个盒子。让(行,列)是位置。
- 如果 (row, col) 处的幻方值为空,则 (i, j) <– (row, col)。
- 如果 magic[row][col] 不为空,则通过将 i 加 1 从位置 (i, j) 向下移动到下一行。但是,在向下移动时,如果我们超出了正方形的边界,则考虑 a广场对面的盒子。
- 在 (i, j) 位置的幻方中插入下一个更大的数字。
- 重复步骤 1,直到所有方块都被填满。
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX = 10;
// Function to print the generated square
void print(int mat[MAX][MAX], int n)
{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << " " << mat[i][j];
}
cout << '\n';
}
}
// Function to generate the magic
// square of order n
void magic_square(int magic[MAX][MAX], int n)
{
// Position of the first value
int i = 0;
int j = (n - 1) / 2;
// First value is placed
// in the magic square
magic[i][j] = 1;
for (int k = 2; k <= n * n; k++) {
// Up position
int row = (i - 1 < 0) ? (n - 1) : (i - 1);
// Left position
int col = (j - 1 < 0) ? (n - 1) : (j - 1);
// If no item is present
if (magic[row][col] == 0) {
// Move up and left
i = row, j = col;
}
// Otherwise
else {
// Move downwards
i = (i + 1) % n;
}
// Place the next value
magic[i][j] = k;
}
}
// Driver code
int main()
{
int magic[MAX][MAX] = { 0 };
int n = 3;
if (n % 2 == 1) {
// Generate the magic square
magic_square(magic, n);
// Print the magic square
print(magic, n);
}
return 0;
}
Java
// Java implementation of the approach
class GFG
{
final static int MAX = 10;
// Function to print the generated square
static void print(int mat[][], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(mat[i][j] + " ");
}
System.out.println();
}
}
// Function to generate the magic
// square of order n
static void magic_square(int magic[][], int n)
{
// Position of the first value
int i = 0;
int j = (n - 1) / 2;
// First value is placed
// in the magic square
magic[i][j] = 1;
for (int k = 2; k <= n * n; k++)
{
// Up position
int row = (i - 1 < 0) ?
(n - 1) : (i - 1);
// Left position
int col = (j - 1 < 0) ?
(n - 1) : (j - 1);
// If no item is present
if (magic[row][col] == 0)
{
// Move up and left
i = row; j = col;
}
// Otherwise
else
{
// Move downwards
i = (i + 1) % n;
}
// Place the next value
magic[i][j] = k;
}
}
// Driver code
public static void main (String[] args)
{
int magic[][] = new int[MAX][MAX];
int n = 3;
if (n % 2 == 1)
{
// Generate the magic square
magic_square(magic, n);
// Print the magic square
print(magic, n);
}
}
}
// This code is contributed by AnkitRai01
Python 3
# Python 3 implementation of the approach
MAX = 10
# Function to print the generated square
def printf(mat, n):
for i in range(n):
for j in range(n):
print(mat[i][j], end = " ")
print("\n", end = "")
# Function to generate the magic
# square of order n
def magic_square(magic,n):
# Position of the first value
i = 0
j = (n - 1) // 2
# First value is placed
# in the magic square
magic[i][j] = 1
for k in range(2, n * n + 1, 1):
# Up position
if(i - 1 < 0):
row = (n - 1)
else:
row = (i - 1)
# Left position
if(j - 1 < 0):
col = (n - 1)
else:
col = (j - 1)
# If no item is present
if (magic[row][col] == 0):
# Move up and left
i = row
j = col
# Otherwise
else:
# Move downwards
i = (i + 1) % n
# Place the next value
magic[i][j] = k
# Driver code
if __name__ == '__main__':
magic = [[0 for i in range(MAX)]
for j in range(MAX)]
n = 3
if (n % 2 == 1):
# Generate the magic square
magic_square(magic, n)
# Print the magic square
printf(magic, n)
# This code is contributed by Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX = 10;
// Function to print the generated square
static void print(int [,]mat, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(mat[i, j] + " ");
}
Console.WriteLine();
}
}
// Function to generate the magic
// square of order n
static void magic_square(int [,]magic, int n)
{
// Position of the first value
int i = 0;
int j = (n - 1) / 2;
// First value is placed
// in the magic square
magic[i, j] = 1;
for (int k = 2; k <= n * n; k++)
{
// Up position
int row = (i - 1 < 0) ?
(n - 1) : (i - 1);
// Left position
int col = (j - 1 < 0) ?
(n - 1) : (j - 1);
// If no item is present
if (magic[row, col] == 0)
{
// Move up and left
i = row; j = col;
}
// Otherwise
else
{
// Move downwards
i = (i + 1) % n;
}
// Place the next value
magic[i, j] = k;
}
}
// Driver code
public static void Main(String[] args)
{
int [,]magic = new int[MAX, MAX];
int n = 3;
if (n % 2 == 1)
{
// Generate the magic square
magic_square(magic, n);
// Print the magic square
print(magic, n);
}
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
6 1 8
7 5 3
2 9 4
时间复杂度:O(N ^ 2)。
辅助空间:O(N ^ 2)。