📌  相关文章
📜  为输入 n 构造唯一矩阵 nxn

📅  最后修改于: 2022-05-13 01:57:22.079000             🧑  作者: Mango

为输入 n 构造唯一矩阵 nxn

给定一个奇数 n,找到一个大小为 nxn 的矩阵,条件如下:

  1. 每个单元格包含一个从 1 到 n(包括)的整数。
  2. 没有整数在同一行或同一列中出现两次。
  3. 所有 1 都必须在距矩阵中心的所有可能距离处。 anxn 正方形的中心是单元格 ((n-1)/2, (n-1)/2),表示奇数 n。

例子 :

Input  : n = 1
Output : 1

Input : n = 3
Output: 3 2 1
        1 3 2
        2 1 3

Input : n = 5
Output : 5 3 2 4 1 
         1 4 3 5 2 
         2 5 4 1 3 
         3 1 5 2 4 
         4 2 1 3 5 
 

这个想法是首先确定1的位置。一旦 n = 5 的 1 可能的排列是,

_ _ _ _ 1 
1 _ _ _ _ 
_ _ _ 1 _ 
_ 1 _ _ _ 
_ _ 1 _ _ 

一旦我们确定了 1s,填充剩余项目的任务就很简单了。我们逐列填写剩余条目。对于每个 1,我们遍历它的列并用 2、3、...p 填充它下面的所有条目,并用 p+1、..n 填充它上面的所有条目。我们得到关注。

5 3 2 4 1 
1 4 3 5 2 
2 5 4 1 3 
3 1 5 2 4 
4 2 1 3 5 

为了确定 1 的初始位置,我们遍历所有行并跟踪两个列号“左”和“右”。
1)“右”从 n-1 开始,并在每行后递减。
2)“左”从 0 开始,并在每一行之后保持递增。
以下是上述想法的实现。

C++
// C++ program to construct an n x n
// matrix such that every row and every
// column has distinct values.
#include 
#include 
 
using namespace std;
 
const int MAX = 100;
int mat[MAX][MAX];
 
// Fills non-one entries in column j
// Given that there is a "1" at
// position mat[i][j], this function
// fills other entries of column j.
void fillRemaining(int i, int j, int n)
{
    // Initialize value to be filled
    int x = 2;
 
    // Fill all values below i as 2, 3, ...p
    for (int k = i + 1; k < n; k++)
        mat[k][j] = x++;
 
    // Fill all values above i
    // as p + 1, p + 2, .. n
    for (int k = 0; k < i; k++)
        mat[k][j] = x++;
}
 
// Fills entries in mat[][]
// with the given set of rules
void constructMatrix(int n)
{
    // Alternatively fill 1s starting from
    // rightmost and leftmost columns. For
    // example for n = 3, we get { {_ _ 1},
    // {1 _ _} {_ 1 _}}
    int right = n - 1, left = 0;
    for (int i = 0; i < n; i++)
    {
        // If i is even, then fill 
        // next column from right
        if (i % 2 == 0)
        {
            mat[i][right] = 1;
 
            // After filling 1, fill remaining
            // entries of column "right"
            fillRemaining(i, right, n);
 
            // Move right one column back
            right--;
        }
         
        // Fill next column from left
        else
        {
            mat[i][left] = 1;
 
            // After filling 1, fill remaining
            // entries of column "left"
            fillRemaining(i, left, n);
 
            // Move left one column forward
            left++;
        }
    }
}
 
// Driver code
int main()
{
    int n = 5;
 
    // Passing n to constructMatrix function
    constructMatrix(n);
 
    // Printing the desired unique matrix
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            printf("%d ",mat[i][j]);
        printf ("\n");
    }
    return 0;
}


Java
// Java program to construct an n x n
// matrix such that every row and every
// column has distinct values.
class GFG
{
     
    static final int MAX = 100;
    static int[][] mat = new int[MAX][MAX];
     
    // Fills non-one entries in column j
    // Given that there is a "1" at
    // position mat[i][j], this function
    // fills other entries of column j.
    static void fillRemaining(int i, int j, int n)
    {
        // Initialize value to be filled
        int x = 2;
     
        // Fill all values below i as 2, 3, ...p
        for (int k = i + 1; k < n; k++)
            mat[k][j] = x++;
     
        // Fill all values above i
        // as p + 1, p + 2, .. n
        for (int k = 0; k < i; k++)
            mat[k][j] = x++;
    }
     
    // Fills entries in mat[][]
    // with the given set of rules
    static void constructMatrix(int n)
    {
        // Alternatively fill 1s starting from
        // rightmost and leftmost columns. For
        // example for n = 3, we get { {_ _ 1},
        // {1 _ _} {_ 1 _}}
        int right = n - 1, left = 0;
        for (int i = 0; i < n; i++)
        {
            // If i is even, then fill
            //  next column from right
            if (i % 2 == 0)
            {
                mat[i][right] = 1;
     
                // After filling 1, fill remaining
                // entries of column "right"
                fillRemaining(i, right, n);
     
                // Move right one column back
                right--;
            }
             
            // Fill next column from left
            else
            {
                mat[i][left] = 1;
     
                // After filling 1, fill remaining
                // entries of column "left"
                fillRemaining(i, left, n);
     
                // Move left one column forward
                left++;
            }
        }
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int n = 5;
     
        // Passing n to constructMatrix function
        constructMatrix(n);
     
        // Printing the desired unique matrix
        for (int i = 0; i < n; i++)
        {
            for (int j = 0 ; j < n; j++)
            System.out.print(mat[i][j]+" ");
            System.out.println();
        }
    }
}
 
// This code is contributed by Sumit Ghosh


Python3
# Python3 program to construct an n x n
# matrix such that every row and every
# column has distinct values.
 
MAX = 100;
mat = [[0 for x in range(MAX)] for y in range(MAX)];
 
# Fills non-one entries in column j
# Given that there is a "1" at
# position mat[i][j], this function
# fills other entries of column j.
def fillRemaining(i, j, n):
 
    # Initialize value to be filled
    x = 2;
 
    # Fill all values below i as 2, 3, ...p
    for k in range(i + 1,n):
        mat[k][j] = x;
        x+=1;
 
    # Fill all values above i
    # as p + 1, p + 2, .. n
    for k in range(i):
        mat[k][j] = x;
        x+=1;
 
# Fills entries in mat[][]
# with the given set of rules
def constructMatrix(n):
 
    # Alternatively fill 1s starting from
    # rightmost and leftmost columns. For
    # example for n = 3, we get { {_ _ 1},
    # {1 _ _} {_ 1 _}}
    right = n - 1;
    left = 0;
    for i in range(n):
        # If i is even, then fill
        # next column from right
        if (i % 2 == 0):
            mat[i][right] = 1;
 
            # After filling 1, fill remaining
            # entries of column "right"
            fillRemaining(i, right, n);
 
            # Move right one column back
            right-=1;
         
        # Fill next column from left
        else:
            mat[i][left] = 1;
 
            # After filling 1, fill remaining
            # entries of column "left"
            fillRemaining(i, left, n);
 
            # Move left one column forward
            left+=1;
 
# Driver code
n = 5;
 
# Passing n to constructMatrix function
constructMatrix(n);
 
# Printing the desired unique matrix
for i in range(n):
    for j in range(n):
        print(mat[i][j],end=" ");
    print("");
     
# This code is contributed by mits


C#
// C# program to construct an n x n
// matrix such that every row and
// every column has distinct values.
using System;
 
class GFG
{
     
    static int MAX = 100;
    static int [,]mat = new int[MAX, MAX];
     
    // Fills non-one entries in column j
    // Given that there is a "1" at
    // position mat[i][j], this function
    // fills other entries of column j.
    static void fillRemaining(int i, int j, int n)
    {
        // Initialize value to be filled
        int x = 2;
     
        // Fill all values below i as 2, 3, ...p
        for (int k = i + 1; k < n; k++)
            mat[k, j] = x++;
     
        // Fill all values above i
        // as p + 1, p + 2, .. n
        for (int k = 0; k < i; k++)
            mat[k, j] = x++;
    }
     
    // Fills entries in mat[][]
    // with the given set of rules
    static void constructMatrix(int n)
    {
        // Alternatively fill 1s starting from
        // rightmost and leftmost columns. For
        // example for n = 3, we get { {_ _ 1},
        // {1 _ _} {_ 1 _}}
        int right = n - 1, left = 0;
        for (int i = 0; i < n; i++)
        {
            // If i is even, then fill
            // next column from right
            if (i % 2 == 0)
            {
                mat[i, right] = 1;
     
                // After filling 1, fill remaining
                // entries of column "right"
                fillRemaining(i, right, n);
     
                // Move right one column back
                right--;
            }
             
            // Fill next column from left
            else
            {
                mat[i, left] = 1;
     
                // After filling 1, fill remaining
                // entries of column "left"
                fillRemaining(i, left, n);
     
                // Move left one column forward
                left++;
            }
        }
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 5;
     
        // Passing n to constructMatrix function
        constructMatrix(n);
     
        // Printing the desired unique matrix
        for (int i = 0; i < n; i++)
        {
            for (int j = 0 ; j < n; j++)
            Console.Write(mat[i, j]+" ");
            Console.WriteLine();
        }
    }
}
 
// This code is contributed by nitin mittal


Javascript


输出 :

5 3 2 4 1 
1 4 3 5 2 
2 5 4 1 3 
3 1 5 2 4 
4 2 1 3 5