📜  为输入N构造具有第一个N ^ 2个自然数的矩阵N x N

📅  最后修改于: 2021-04-23 20:48:50             🧑  作者: 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


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

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