📌  相关文章
📜  根据给定条件计算给定二进制矩阵中的映射数

📅  最后修改于: 2022-05-13 01:57:23.980000             🧑  作者: Mango

根据给定条件计算给定二进制矩阵中的映射数

给定一个大小为M*N的二进制矩阵,仅包含01 。任务是计算矩阵中的映射数量。如果满足以下条件,则任意两个1之间存在一个映射:

  • 两个1位于两个不同的行: r1r2 ,其中r1 < r2
  • 对于r1 < i < r2的每一行i ,第 i 行中没有1

例子:

方法:想法是遍历每一行并计算其中1的数量。遍历后续行并在下一行的第一行至少包含一个1时停止。计算这两行之间的映射数量并添加到sum变量。通过将最新的行作为起始参考点再次重复此过程。请按照以下步骤解决给定的问题。

  • 将第一行视为前一行并计算其中的1 ,即prev =第一行中 1 的计数。
  • 在后续行中计数1并在一行中至少有一个1时停止,即next=后续行中的 1 计数。
  • 计算上一行和下一行之间的映射,即maps = prev * next
  • 将新映射的数量添加到 sum 变量中,即res += maps。
  • 将下一行设为上一行并重复查找下一行的循环。
  • 最后,打印总映射数。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
#include 
using namespace std;
 
// Function to count the number of mappings
int CountNumberOfMappings(vector >& matrix)
{
    int i, j, prev = 0, m = matrix.size(),
              n = matrix[0].size();
 
    // Count 1s in first row.
    for (i = 0; i < n; i++) {
        if (matrix[0][i] == 1)
            prev += 1;
    }
 
    // Count 1s in subsequent rows.
    int next = 0, res = 0;
    for (j = 1; j < m; j++) {
        next = 0;
        for (i = 0; i < n; i++) {
            if (matrix[j][i] == 1)
                next += 1;
        }
 
        // Stop when a row has
        // atleast one 1 in it.
        if (next > 0) {
            // Compute number of mappings
            // between prev and next rows.
            res += prev * next;
 
            // Add these mappings to
            // result variable.
            prev = next;
        }
    }
 
    // Return total number of mappings.
    return res;
}
 
// Driver Code
int main()
{
 
    vector > matrix{ { 0, 1, 1, 0, 0, 1 },
                                 { 0, 0, 0, 0, 0, 0 },
                                 { 0, 1, 0, 1, 0, 0 },
                                 { 0, 0, 1, 0, 0, 0 } };
 
    int res = CountNumberOfMappings(matrix);
    cout << res;
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
 
  // Function to count the number of mappings
  static int CountNumberOfMappings(int[][] matrix)
  {
    int i, j, prev = 0, m = matrix.length,
    n = matrix[0].length;
 
    // Count 1s in first row.
    for (i = 0; i < n; i++) {
      if (matrix[0][i] == 1)
        prev += 1;
    }
 
    // Count 1s in subsequent rows.
    int next = 0, res = 0;
    for (j = 1; j < m; j++) {
      next = 0;
      for (i = 0; i < n; i++) {
        if (matrix[j][i] == 1)
          next += 1;
      }
 
      // Stop when a row has
      // atleast one 1 in it.
      if (next > 0)
      {
 
        // Compute number of mappings
        // between prev and next rows.
        res += prev * next;
 
        // Add these mappings to
        // result variable.
        prev = next;
      }
    }
 
    // Return total number of mappings.
    return res;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int[][] matrix = { { 0, 1, 1, 0, 0, 1 },
                      { 0, 0, 0, 0, 0, 0 },
                      { 0, 1, 0, 1, 0, 0 },
                      { 0, 0, 1, 0, 0, 0 } };
 
    int res = CountNumberOfMappings(matrix);
    System.out.print(res);
  }
}
 
// This code is contributed by hrithikgarg03188


Python3
# Python code for the above approach
 
# Function to count the number of mappings
def CountNumberOfMappings(matrix):
    prev = 0
    m = len(matrix)
    n = len(matrix[0])
 
    # Count 1s in first row.
    for i in range(n):
        if (matrix[0][i] == 1):
            prev += 1
 
    # Count 1s in subsequent rows.
    next = 0
    res = 0
    for j in range(1, m):
        next = 0
        for i in range(n):
            if (matrix[j][i] == 1):
                next += 1
 
        # Stop when a row has
        # atleast one 1 in it.
        if (next > 0):
            # Compute number of mappings
            # between prev and next rows.
            res += prev * next
 
            # Add these mappings to
            # result variable.
            prev = next
 
    # Return total number of mappings.
    return res
 
# Driver Code
matrix = [[0, 1, 1, 0, 0, 1],
          [0, 0, 0, 0, 0, 0],
          [0, 1, 0, 1, 0, 0],
          [0, 0, 1, 0, 0, 0]]
 
res = CountNumberOfMappings(matrix)
print(res)
 
# This code is contributed by gfgking


C#
// C# program for above approach
using System;
class GFG
{
 
  // Function to count the number of mappings
  static int CountNumberOfMappings(int[, ] matrix)
  {
    int prev = 0;
    int m = matrix.GetLength(0), n
      = matrix.GetLength(1);
 
    // Count 1s in first row.
    for (int i = 0; i < n; i++) {
      if (matrix[0, i] == 1)
        prev += 1;
    }
 
    // Count 1s in subsequent rows.
    int next = 0, res = 0;
    for (int j = 1; j < m; j++) {
      next = 0;
      for (int i = 0; i < n; i++) {
        if (matrix[j, i] == 1)
          next += 1;
      }
 
      // Stop when a row has
      // atleast one 1 in it.
      if (next > 0) {
        // Compute number of mappings
        // between prev and next rows.
        res += prev * next;
 
        // Add these mappings to
        // result variable.
        prev = next;
      }
    }
 
    // Return total number of mappings.
    return res;
  }
 
  // Driver Code
  public static void Main()
  {
 
    int[, ] matrix = { { 0, 1, 1, 0, 0, 1 },
                      { 0, 0, 0, 0, 0, 0 },
                      { 0, 1, 0, 1, 0, 0 },
                      { 0, 0, 1, 0, 0, 0 } };
 
    int res = CountNumberOfMappings(matrix);
    Console.Write(res);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出:
8

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