📜  用前 N^2 个自然数为输入 N 构造一个矩阵 N x N

📅  最后修改于: 2021-09-06 17:47:13             🧑  作者: Mango

给定一个整数N ,任务是构造一个大小为N x N的矩阵M[][] ,其中的数字范围为[1, N^2] ,条件如下:

  1. 矩阵 M 的元素应该是 1 到 N^2 之间的整数。
  2. 矩阵 M 的所有元素都是成对不同的。
  3. 对于包含行 r 到 r+a 和列 c 到 c+a(含)中的单元格的每个方形子矩阵,对于某些有效整数 r,c 和 a>=0: M(r,c)+M(r+a, c+a) 是偶数,M(r,c+a)+M(r+a,c) 是偶数。

例子:

方法:我们知道,当两个数的奇偶性相同时,它们的和是偶数。假设M (i, j)的奇偶性是奇数,这意味着M (i+1, j+1) , M (i+1, j-1) , M (i-1, j+1 )的奇偶性) , M (i-1, j-1)必须是奇数。
下面是 N = 4 生成大小为 4×4 的矩阵的说明:

所以从上面的插图中,我们必须在棋盘图案中填充矩阵。我们可以通过两种方式填写:

  • 所有黑色单元格都有一个奇数整数,而白色单元格有一个偶数整数。
  • 所有黑色单元格都有一个偶数,而白色单元格有一个奇数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to print the desired matrix
void UniqueMatrix(int N)
{
    int element_value = 1;
    int i = 0;
 
    // element_value will start from 1
    // and go up to N ^ 2
 
    // i is row number and it starts
    // from 0 and go up to N-1
     
    // Iterate ove all [0, N]
    while(i < N)
    {
         
        // If is even
        if(i % 2 == 0)
        {
            for(int f = element_value;
                    f < element_value + N; f++)
            {
                 
                // If row number is even print
                // the row in forward order
                cout << f << " ";
            }
            element_value += N;
        }
        else
        {
            for(int k = element_value + N - 1;
                    k > element_value - 1; k--)
            {
                 
                // If row number is odd print
                // the row in reversed order
                cout << k << " ";
 
            }
            element_value += N;
        }
        cout << endl;
        i = i + 1;
    }
}
 
// Driver Code
int main()
{
     
    // Given matrix size
    int N = 4;
 
    // Function call
    UniqueMatrix(N);
}
 
// This code is contributed by chitranayal


Java
// Java program for the above approach
public class Gfg
{
   
    // Function to print the desired matrix
    public static void UniqueMatrix(int N)
    {
        int element_value = 1;
        int i = 0;
         
        // element_value will start from 1
        // and go up to N ^ 2
   
        // i is row number and it starts
        // from 0 and go up to N-1
       
        // Iterate ove all [0, N]
        while(i < N)
        {
            // If is even
            if(i % 2 == 0)
            {
                for(int f = element_value;
                    f < element_value + N; f++)
                {
                   
                    // If row number is even print
                    // the row in forward order
                    System.out.print(f+" ");
                }
                element_value += N;
            }
            else
            {
                for(int k = element_value + N - 1;
                    k > element_value - 1; k--)
                {
                   
                    // If row number is odd print
                    // the row in reversed order
                    System.out.print(k+" ");
                }
                element_value += N;
            }
            System.out.println();
            i = i + 1;
        }
    }
   
    // Driver Code
    public static void main(String []args)
    {
       
        // Given matrix size
        int N = 4;
       
        // Function call
        UniqueMatrix(N);
    }
}
 
// This code is contributed by avanitrachhadiya2155


Python3
# Python3 program for the above approach
 
# Function to print the desired matrix
def UniqueMatrix(N):
 
    element_value = 1
    i = 0
 
    # element_value will start from 1
    # and go up to N ^ 2
 
    # i is row number and it starts
    # from 0 and go up to N-1
     
    # Iterate ove all [0, N]
    while(i < N):
         
        # If is even
        if(i % 2 == 0):
 
            for f in range(element_value, element_value + N, 1):
 
                # If row number is even print
                # the row in forward order
                print(f, end =' ')
            element_value += N
 
        else:
 
            for k in range(element_value + N-1, element_value-1, -1):
 
                # if row number is odd print
                # the row in reversed order
                print(k, end =' ')
 
            element_value += N
 
        print()
        i = i + 1
 
 
# Driver Code
 
# Given Matrix Size
N = 4
 
# Function Call
UniqueMatrix(N)


C#
// C# program for the above approach
using System;
class GFG
{
    static void UniqueMatrix(int N)
    {
        int element_value = 1;
        int i = 0;
          
        // element_value will start from 1
        // and go up to N ^ 2
    
        // i is row number and it starts
        // from 0 and go up to N-1
        
        // Iterate ove all [0, N]
        while(i < N)
        {
           
            // If is even
            if(i % 2 == 0)
            {
                for(int f = element_value;
                    f < element_value + N; f++)
                {
                   
                    // If row number is even print
                    // the row in forward order
                    Console.Write(f + " ");
                }
                element_value += N;
            }
            else
            {
                for(int k = element_value + N - 1;
                    k > element_value - 1; k--)
                {
                    Console.Write(k + " ");
                }
                element_value += N;
            }
            Console.WriteLine();
            i = i + 1;
        }
    }
   
  // Driver code
  static public void Main ()
  {
    // Given matrix size
    int N = 4;
 
    // Function call
    UniqueMatrix(N);
  }
}
 
// This code is contributed by rag2127


Javascript


输出:
1 2 3 4 
8 7 6 5 
9 10 11 12 
16 15 14 13

时间复杂度: O(N^2)
辅助空间: O(N^2)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live