📌  相关文章
📜  给定行和列中的最大元素时,找到原始矩阵

📅  最后修改于: 2021-05-04 08:14:11             🧑  作者: Mango

给定两个分别由NM个整数组成的数组A []B [] 。还给出了一个NXM二进制矩阵,其中1表示原始矩阵中存在一个正整数,0表示该位置在原始矩阵中填充为0。的任务是,以形成回原始矩阵,使得A [i]指示最大元素的第i行中和B [j]指示在j列中的最大元素。

例子:

Input: A[] = {2, 1, 3}, B[] = {2, 3, 0, 0, 2, 0, 1},
matrix[] = {{1, 0, 0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0, 0, 1},
            {1, 1, 0, 0, 0, 0, 0}}

Output: 
2 0 0 0 2 0 0 
0 0 0 0 0 0 1 
2 3 0 0 0 0 0

Input: A[] = {2, 4}, B[] = {4, 2},
matrix[] = {{1, 1},
            {1, 1}}
Output:
2 2
4 2

方法:对矩阵中的每个索引(i,j)进行迭代,如果mat [i] [j] == 1 ,则用min(A [i],B [j])填充位置。这是因为当前元素是第i部分行和j,并且如果最大值(A [I],B [j]的)被选然后的条件之一不能被满足,即所选择的元件可超过当前行或当前列中所需的最大元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 3
#define M 7
  
// Function that prints the original matrix
void printOriginalMatrix(int a[], int b[], int mat[N][M])
{
    // Iterate in the row
    for (int i = 0; i < N; i++) {
  
        // Iterate in the column
        for (int j = 0; j < M; j++) {
  
            // If previously existed an element
            if (mat[i][j] == 1)
                cout << min(a[i], b[j]) << " ";
            else
                cout << 0 << " ";
        }
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int a[] = { 2, 1, 3 };
    int b[] = { 2, 3, 0, 0, 2, 0, 1 };
    int mat[N][M] = { { 1, 0, 0, 0, 1, 0, 0 },
                      { 0, 0, 0, 0, 0, 0, 1 },
                      { 1, 1, 0, 0, 0, 0, 0 } };
    printOriginalMatrix(a, b, mat);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
static int N = 3;
static int M = 7;
  
// Function that prints the original matrix
static void printOriginalMatrix(int a[], int b[], 
                                int[][] mat)
{
  
    // Iterate in the row
    for (int i = 0; i < N; i++) 
    {
  
        // Iterate in the column
        for (int j = 0; j < M; j++) 
        {
  
            // If previously existed an element
            if (mat[i][j] == 1)
                System.out.print(Math.min(a[i], 
                                          b[j]) + " ");
            else
                System.out.print("0" + " ");
        }
        System.out.println();
    }
}
  
// Driver code
public static void main(String[] args)
{
    int a[] = { 2, 1, 3 };
    int b[] = { 2, 3, 0, 0, 2, 0, 1 };
    int[][] mat = {{ 1, 0, 0, 0, 1, 0, 0 },
                   { 0, 0, 0, 0, 0, 0, 1 },
                   { 1, 1, 0, 0, 0, 0, 0 }};
    printOriginalMatrix(a, b, mat);
}
}
  
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
N = 3
M = 7
  
# Function that prints the original matrix 
def printOriginalMatrix(a, b, mat) : 
  
    # Iterate in the row 
    for i in range(N) : 
  
        # Iterate in the column 
        for j in range(M) :
  
            # If previously existed an element 
            if (mat[i][j] == 1) :
                print(min(a[i], b[j]), end = " "); 
            else :
                print(0, end = " "); 
          
        print()
  
# Driver code 
if __name__ == "__main__" : 
  
    a = [ 2, 1, 3 ]
    b = [ 2, 3, 0, 0, 2, 0, 1 ]
      
    mat = [[ 1, 0, 0, 0, 1, 0, 0 ], 
           [ 0, 0, 0, 0, 0, 0, 1 ], 
           [ 1, 1, 0, 0, 0, 0, 0 ]]; 
              
    printOriginalMatrix(a, b, mat); 
  
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
static int N = 3;
static int M = 7;
  
// Function that prints the original matrix
static void printOriginalMatrix(int[] a, int[] b, 
                                int[,] mat)
{
  
    // Iterate in the row
    for (int i = 0; i < N; i++) 
    {
  
        // Iterate in the column
        for (int j = 0; j < M; j++) 
        {
  
            // If previously existed an element
            if (mat[i,j] == 1)
                Console.Write(Math.Min(a[i], 
                                        b[j]) + " ");
            else
                Console.Write("0" + " ");
        }
        Console.WriteLine();
    }
}
  
// Driver code
public static void Main()
{
    int[] a = { 2, 1, 3 };
    int[] b = { 2, 3, 0, 0, 2, 0, 1 };
    int[,] mat = {{ 1, 0, 0, 0, 1, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 1 },
                { 1, 1, 0, 0, 0, 0, 0 }};
    printOriginalMatrix(a, b, mat);
}
}
  
// This code is contributed by Code_Mech


PHP


输出:
2 0 0 0 2 0 0 
0 0 0 0 0 0 1 
2 3 0 0 0 0 0