📌  相关文章
📜  查找一个N x N网格,其每行和每列的xor相等

📅  最后修改于: 2021-04-26 05:06:46             🧑  作者: Mango

给定一个整数N(它是4的倍数),任务是找到一个N x N的网格,对于该网格,每一行和每一列的按位xor都相同。

例子:

方法:解决此问题的方法是将每行和每一列的xor固定为0,因为从0开始的4个连续数字的xor为0。这是一个4 x 4矩阵的示例:

如果在上面的示例中注意到,每个行和列的异或为0。现在我们需要以这样的方式放置数字:每个行和列的异或为0。因此,我们可以将N x N矩阵划分为N / 4行和列的较小的4 x 4矩阵,并以每行和列的xor为0的方式填充单元格。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find the n x n matrix
// that satisfies the given condition
void findGrid(int n)
{
    int arr[n][n];
  
    // Initialize x to 0
    int x = 0;
  
    // Divide the n x n matrix into n / 4 matrices
    // for each of the n / 4 rows where
    // each matrix is of size 4 x 4
    for (int i = 0; i < n / 4; i++) {
        for (int j = 0; j < n / 4; j++) {
            for (int k = 0; k < 4; k++) {
                for (int l = 0; l < 4; l++) {
                    arr[i * 4 + k][j * 4 + l] = x;
                    x++;
                }
            }
        }
    }
  
    // Print the generated matrix
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << arr[i][j] << " ";
        }
        cout << "\n";
    }
}
  
// Driver code
int main()
{
    int n = 4;
  
    findGrid(n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
      
// Function to find the n x n matrix
// that satisfies the given condition
static void findGrid(int n)
{
    int [][]arr = new int[n][n];
  
    // Initialize x to 0
    int x = 0;
  
    // Divide the n x n matrix into n / 4 matrices
    // for each of the n / 4 rows where
    // each matrix is of size 4 x 4
    for (int i = 0; i < n / 4; i++)
    {
        for (int j = 0; j < n / 4; j++) 
        {
            for (int k = 0; k < 4; k++) 
            {
                for (int l = 0; l < 4; l++) 
                {
                    arr[i * 4 + k][j * 4 + l] = x;
                    x++;
                }
            }
        }
    }
  
    // Print the generated matrix
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println(" ");
    }
}
  
// Driver code
public static void main (String[] args)
{
    int n = 4;
      
    findGrid(n);
}
}
  
// This code is contributed by ajit.


Python3
# Python3 implementation of the approach 
  
# Function to find the n x n matrix 
# that satisfies the given condition 
def findGrid(n): 
  
    arr = [[0 for k in range(n)] 
              for l in range(n)] 
  
    # Initialize x to 0 
    x = 0
  
    # Divide the n x n matrix into n / 4 matrices 
    # for each of the n / 4 rows where 
    # each matrix is of size 4 x 4 
    for i in range(n // 4): 
        for j in range(n // 4): 
            for k in range(4): 
                for l in range(4): 
                    arr[i * 4 + k][j * 4 + l] = x 
                    x += 1
  
    # Print the generated matrix 
    for i in range(n): 
        for j in range(n): 
            print(arr[i][j], end = " ")
        print()
  
# Driver code 
n = 4
findGrid(n) 
  
# This code is contributed by divyamohan123


C#
// C# implementation of the approach
using System;
      
class GFG 
{
      
// Function to find the n x n matrix
// that satisfies the given condition
static void findGrid(int n)
{
    int [,]arr = new int[n, n];
  
    // Initialize x to 0
    int x = 0;
  
    // Divide the n x n matrix into n / 4 matrices
    // for each of the n / 4 rows where
    // each matrix is of size 4 x 4
    for (int i = 0; i < n / 4; i++)
    {
        for (int j = 0; j < n / 4; j++) 
        {
            for (int k = 0; k < 4; k++) 
            {
                for (int l = 0; l < 4; l++) 
                {
                    arr[i * 4 + k, j * 4 + l] = x;
                    x++;
                }
            }
        }
    }
  
    // Print the generated matrix
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < n; j++) 
        {
            Console.Write(arr[i, j] + " ");
        }
        Console.WriteLine(" ");
    }
}
  
// Driver code
public static void Main (String[] args)
{
    int n = 4;
      
    findGrid(n);
}
}
  
// This code is contributed by PrinciRaj1992


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

时间复杂度: O(N 2 )