📌  相关文章
📜  生成一个矩阵,使给定的Matrix元素等于生成的Matrix的所有相应行和列元素的按位或

📅  最后修改于: 2021-04-22 00:04:06             🧑  作者: Mango

给定维度为N * M的矩阵B [] [] ,任务是生成具有相同维度的矩阵A [] [] ,该矩阵可以形成为使得任何元素B [i] [j]等于按位或的第ij A [] []的列中的所有元素。如果不存在这样的矩阵,则打印“不可能”。否则,打印矩阵A [] []

例子:

方法:思想是基于以下观察:如果任何元素B [i] [j] = 0,则所有的元素i行和j矩阵的列A [] []将是0,因为仅按位0s组合的OR得出0 。请按照以下步骤解决问题:

  • 创建一个大小为N * M的矩阵A [] [] ,并使用1初始化其所有元素。
  • 遍历矩阵B [] []逐行,使用变量ij以及如果B [i] [j] = 0,则使的所有元素行和j矩阵的列A [] []作为0
  • 遍历矩阵A [] []逐行,使用变量ij以及每一个索引(i,j)中,发现在逐位的元件的OR i行和j矩阵A[] []并将其存储在变量c中。如果c不等于B [i] [j] ,则打印“不可能”并退出循环。
  • 完成上述步骤后,如果未遇到break语句,则打印生成的矩阵A [] []

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the matrix, A[][]
// satisfying the given conditions
void findOriginalMatrix(
    vector > B, int N, int M)
{
    // Store the final matrix
    int A[N][M];
 
    // Initialize all the elements of
    // the matrix A with 1
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            A[i][j] = 1;
        }
    }
 
    // Traverse the matrix B[][] row-wise
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
 
            // If B[i][j] is equal to 0
            if (B[i][j] == 0) {
 
                // Mark all the elements of
                // ith row of A[][] as 0
                for (int k = 0; k < M; ++k) {
                    A[i][k] = 0;
                }
 
                // Mark all the elements of
                // jth column of A[][] as 0
                for (int k = 0; k < N; ++k) {
                    A[k][j] = 0;
                }
            }
        }
    }
 
    // Check if the matrix B[][] can
    // be made using matrix A[][]
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
 
            // Store the bitwise OR of
            // all elements of A[][] in
            // ith row and jth column
            int c = 0;
 
            // Traverse through ith row
            for (int k = 0; k < M; ++k) {
                if (c == 1)
                    break;
                c += A[i][k];
            }
 
            // Traverse through jth column
            for (int k = 0; k < N; ++k) {
                if (c == 1)
                    break;
                c += A[k][j];
            }
 
            // If B[i][j] is not equal to
            // c, print "Not Possible"
            if (c != B[i][j]) {
                cout << "Not Possible";
                return;
            }
        }
    }
 
    // Print the final matrix A[][]
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            cout << A[i][j] << " ";
        }
        cout << "\n";
    }
}
 
// Driver Code
int main()
{
    vector > B{ { 1, 1, 1 }, { 1, 1, 1 } };
 
    int N = B.size();
    int M = B[0].size();
 
    // Function Call
    findOriginalMatrix(B, N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the matrix, A[][]
// satisfying the given conditions
static void findOriginalMatrix(int[][] B, int N,
                               int M)
{
     
    // Store the final matrix
    int[][] A = new int[N][M];
 
    // Initialize all the elements of
    // the matrix A with 1
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            A[i][j] = 1;
        }
    }
 
    // Traverse the matrix B[][] row-wise
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
             
            // If B[i][j] is equal to 0
            if (B[i][j] == 0)
            {
                 
                // Mark all the elements of
                // ith row of A[][] as 0
                for(int k = 0; k < M; ++k)
                {
                    A[i][k] = 0;
                }
 
                // Mark all the elements of
                // jth column of A[][] as 0
                for(int k = 0; k < N; ++k)
                {
                    A[k][j] = 0;
                }
            }
        }
    }
 
    // Check if the matrix B[][] can
    // be made using matrix A[][]
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
             
            // Store the bitwise OR of
            // all elements of A[][] in
            // ith row and jth column
            int c = 0;
 
            // Traverse through ith row
            for(int k = 0; k < M; ++k)
            {
                if (c == 1)
                    break;
                     
                c += A[i][k];
            }
 
            // Traverse through jth column
            for(int k = 0; k < N; ++k)
            {
                if (c == 1)
                    break;
                     
                c += A[k][j];
            }
 
            // If B[i][j] is not equal to
            // c, print "Not Possible"
            if (c != B[i][j])
            {
                System.out.println("Not Possible");
                return;
            }
        }
    }
 
    // Print the final matrix A[][]
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            System.out.print(A[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[][] B = new int[][]{ { 1, 1, 1 },
                             { 1, 1, 1 } };
 
    int N = B.length;
    int M = B[0].length;
 
    // Function Call
    findOriginalMatrix(B, N, M);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
 
# Function to find the matrix, A[][]
# satisfying the given conditions
def findOriginalMatrix(B, N, M) :
   
    # Store the final matrix
    A = [[0]*M]*N
 
    # Initialize all the elements of
    # the matrix A with 1
    for i in range(N) :
        for j in range(M):
            A[i][j] = 1
         
    # Traverse the matrix B[][] row-wise
    for i in range(N) :
        for j in range(M):
 
            # If B[i][j] is equal to 0
            if (B[i][j] == 0) :
 
                # Mark all the elements of
                # ith row of A[][] as 0
                for k in range(M):
                    A[i][k] = 0
                 
                # Mark all the elements of
                # jth column of A[][] as 0
                for k in range(N):
                    A[k][j] = 0
     
    # Check if the matrix B[][] can
    # be made using matrix A[][]
    for i in range(N) :
        for j in range(M):
 
            # Store the bitwise OR of
            # all elements of A[][] in
            # ith row and jth column
            c = 0
 
            # Traverse through ith row
            for k in range(M):
                if (c == 1) :
                    break
                c += A[i][k]
             
            # Traverse through jth column
            for k in range(N):
                if (c == 1) :
                    break
                c += A[k][j]
             
            # If B[i][j] is not equal to
            # c, pr"Not Possible"
            if (c != B[i][j]) :
                print("Not Possible")
                return
 
    # Prthe final matrix A[][]
    for i in range(N) :
        for j in range(M):
            print(A[i][j], end = " ")
         
        print()
     
# Driver Code
B = [[ 1, 1, 1 ], [ 1, 1, 1 ]]
 
N = len(B)
M = len(B[0])
 
# Function Call
findOriginalMatrix(B, N, M)
 
# This code is contributed by splevel62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the matrix, A[][]
// satisfying the given conditions
static void findOriginalMatrix(int[,] B, int N,
                               int M)
{
     
    // Store the final matrix
    int[,] A = new int[N, M];
 
    // Initialize all the elements of
    // the matrix A with 1
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            A[i, j] = 1;
        }
    }
 
    // Traverse the matrix B[][] row-wise
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
             
            // If B[i][j] is equal to 0
            if (B[i, j] == 0)
            {
                 
                // Mark all the elements of
                // ith row of A[][] as 0
                for(int k = 0; k < M; ++k)
                {
                    A[i, k] = 0;
                }
 
                // Mark all the elements of
                // jth column of A[][] as 0
                for(int k = 0; k < N; ++k)
                {
                    A[k, j] = 0;
                }
            }
        }
    }
 
    // Check if the matrix B[][] can
    // be made using matrix A[][]
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
             
            // Store the bitwise OR of
            // all elements of A[][] in
            // ith row and jth column
            int c = 0;
 
            // Traverse through ith row
            for(int k = 0; k < M; ++k)
            {
                if (c == 1)
                    break;
                     
                c += A[i, k];
            }
 
            // Traverse through jth column
            for(int k = 0; k < N; ++k)
            {
                if (c == 1)
                    break;
                     
                c += A[k, j];
            }
 
            // If B[i][j] is not equal to
            // c, print "Not Possible"
            if (c != B[i, j])
            {
                Console.WriteLine("Not Possible");
                return;
            }
        }
    }
 
    // Print the final matrix A[][]
    for(int i = 0; i < N; ++i)
    {
        for(int j = 0; j < M; ++j)
        {
            Console.Write(A[i, j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
static public void Main()
{
    int[,] B = new int[,]{ { 1, 1, 1 },
                           { 1, 1, 1 } };
 
    int N = B.GetLength(0);
    int M = B.GetLength(1);
 
    // Function Call
    findOriginalMatrix(B, N, M);
}
}
 
// This code is contributed by Dharanendra L V


输出:
1 1 1 
1 1 1

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