给定维度为N * M的矩阵B[][] ,任务是生成具有相同维度的矩阵A[][] ,该矩阵可以形成为使得对于任何元素B[i][j]等于按位或A[][] 的第i行第j列中的所有元素。如果不存在这样的矩阵,则打印“不可能”。否则,打印矩阵A[][] 。
例子:
Input: B[][] = {{1, 1, 1}, {1, 1, 1}}
Output:
1 1 1
1 1 1
Explanation:
B[0][0] = 1 = bitwise OR of all elements in 0th row and 0th column of A[][].
B[0][1] = 1 = bitwise OR of all elements in 0th row and 1th column of A[][].
B[0][2] = 1 = bitwise OR of all elements in 0th row and 2th column of A[][].
B[1][0] = 1 = bitwise OR of all elements in 1th row and 0th column of A[][].
B[1][1] = 1 = bitwise OR of all elements in 1th row and 1th column of A[][].
B[1][2] = 1 = bitwise OR of all elements in 1th row and 2th column of A[][].
Input: B[][] = {{1, 0, 0}, {0, 0, 0}}
Output: Not Possible
方法:思想是基于以下观察:如果任何元素B [i] [j] = 0,则所有的元素i行和第j矩阵的列A [] []将是0,因为仅按位0组合的OR结果为0 。请按照以下步骤解决问题:
- 创建一个大小为N*M的矩阵A[][]并用1初始化其所有元素。
- 遍历矩阵B [] []逐行,使用变量i和j以及如果B [i] [j] = 0,则使我的所有元素行和第j个矩阵的列A [] []作为0 .
- 遍历矩阵A [] []逐行,使用变量i和j以及每一个索引(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
# Print the 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
Javascript
1 1 1
1 1 1
时间复杂度: O(N*M 2 )
辅助空间: O(N 2 )
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live