📜  二进制矩阵中给定维度的子矩阵中存在的最小 1 数

📅  最后修改于: 2021-09-06 06:47:50             🧑  作者: Mango

给定一个大小为N × M的二维二进制矩阵mat[][]和两个整数A, B ,任务是找到维度A × BB × A的子矩阵中存在的最少数量的1

例子:

方法:为了解决这个问题,想法是打印维度为 A * B 和 B * A 的所有可能的子矩阵,并且对于每个子矩阵,计算它们中存在的1的数量。

请按照以下步骤解决给定的问题:

  • 初始化一个变量,比如最小值,以存储1秒的最小计数。
  • 遍历矩阵mat[][] 的每个单元格(i, j)和每个(i, j)
    • 如果i + A小于等于Nj + B小于等于M ,则执行以下操作:
      • 初始化一个变量,比如count,以将1的计数存储在子矩阵{mat[i][j], mat[i + A][j + B]} 中
      • 遍历子矩阵的每个单元格并将1的数量存储在变量count 中。
      • 检查计数值是否小于当前最小值。如果发现为真,则更新 minimum 等于count
    • 如果i + B小于等于Nj + A小于等于M ,则执行以下操作:
      • 初始化一个变量,比如count,以在子矩阵{mat[i][j], mat[i + B][j + A]} 中存储1的计数。
      • 遍历子矩阵的每个单元格并在count 中存储1数量。
      • 检查计数值是否小于当前最小值。如果发现为真,则更新 minimum 等于count
  • 最后,打印最小值作为最终结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
#define P 51
 
// Function to count number of 1s
// present in a sub matrix from
// (start_i, start_j) to (end_i, end_j)
int count1s(int start_i, int start_j,
            int end_i, int end_j, int mat[][P])
{
 
    // Stores the number of 1s
    // present in current submatrix
    int count = 0;
 
    // Traverse the submatrix
    for (int x = start_i; x < end_i; x++) {
        for (int y = start_j; y < end_j; y++) {
 
            // If mat[x][y] is equal to 1
            if (mat[x][y] == 1)
 
                // Increase count by 1
                count++;
        }
    }
 
    // Return the total count of 1s
    return count;
}
 
// Function to find the minimum number of 1s
// present in a sub-matrix of size A * B or B * A
int findMinimumCount(int N, int M, int A, int B,
                     int mat[][P])
{
 
    // Stores the minimum count of 1s
    int minimum = 1e9;
 
    // Iterate i from 0 to N
    for (int i = 0; i < N; i++) {
 
        // Iterate j from 0 to M
        for (int j = 0; j < M; j++) {
 
            // If a valid sub matrix of size
            // A * B from (i, j) is possible
            if (i + A <= N && j + B <= M) {
 
                // Count the number of 1s
                // present in the sub matrix
                // of size A * B from (i, j)
                int count
                    = count1s(i, j, i + A, j + B, mat);
 
                // Update minimum if count is
                // less than the current minimum
                minimum = min(count, minimum);
            }
 
            // If a valid sub matrix of size
            // B * A from (i, j) is possible
            if (i + B <= N && j + A <= M) {
 
                // Count the number of 1s in the
                // sub matrix of size B * A from (i, j)
                int count
                    = count1s(i, j, i + B, j + A, mat);
 
                // Update minimum if count is
                // less than the current minimum
                minimum = min(count, minimum);
            }
        }
    }
 
    // Return minimum as the final result
    return minimum;
}
 
// Driver Code
int main()
{
    // Given Input
    int A = 2, B = 2;
    int N = 3, M = 4;
    int mat[P][P] = { { 1, 0, 1, 0 },
                      { 0, 1, 0, 1 },
                      { 1, 0, 1, 0 } };
 
    // Function call to find the minimum number
    // of 1s in a submatrix of size A * B or B * A
    cout << findMinimumCount(N, M, A, B, mat);
}


Java
// Java program for the above approach
class GFG{
 
// Function to count number of 1s
// present in a sub matrix from
// (start_i, start_j) to (end_i, end_j)
static int count1s(int start_i, int start_j,
                   int end_i, int end_j,
                   int[][] mat)
{
     
    // Stores the number of 1s
    // present in current submatrix
    int count = 0;
 
    // Traverse the submatrix
    for(int x = start_i; x < end_i; x++)
    {
        for(int y = start_j; y < end_j; y++)
        {
             
            // If mat[x][y] is equal to 1
            if (mat[x][y] == 1)
 
                // Increase count by 1
                count++;
        }
    }
 
    // Return the total count of 1s
    return count;
}
 
// Function to find the minimum number of 1s
// present in a sub-matrix of size A * B or B * A
static int findMinimumCount(int N, int M, int A,
                            int B, int[][] mat)
{
     
    // Stores the minimum count of 1s
    int minimum = (int) 1e9;
 
    // Iterate i from 0 to N
    for(int i = 0; i < N; i++)
    {
         
        // Iterate j from 0 to M
        for(int j = 0; j < M; j++)
        {
             
            // If a valid sub matrix of size
            // A * B from (i, j) is possible
            if (i + A <= N && j + B <= M)
            {
                 
                // Count the number of 1s
                // present in the sub matrix
                // of size A * B from (i, j)
                int count = count1s(i, j, i + A,
                                    j + B, mat);
 
                // Update minimum if count is
                // less than the current minimum
                minimum = Math.min(count, minimum);
            }
 
            // If a valid sub matrix of size
            // B * A from (i, j) is possible
            if (i + B <= N && j + A <= M)
            {
                 
                // Count the number of 1s in the
                // sub matrix of size B * A from (i, j)
                int count = count1s(i, j, i + B,
                                    j + A, mat);
 
                // Update minimum if count is
                // less than the current minimum
                minimum = Math.min(count, minimum);
            }
        }
    }
 
    // Return minimum as the final result
    return minimum;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given Input
    int A = 2, B = 2;
    int N = 3, M = 4;
    int[][] mat = { { 1, 0, 1, 0 },
                    { 0, 1, 0, 1 },
                    { 1, 0, 1, 0 } };
 
    // Function call to find the minimum number
    // of 1s in a submatrix of size A * B or B * A
    System.out.println(findMinimumCount(N, M, A, B, mat));
}
}
 
// This code is contributed by user_qa7r


Python3
# Python3 program for the above approach
P = 51
 
# Function to count number of 1s
# present in a sub matrix from
# (start_i, start_j) to (end_i, end_j)
def count1s(start_i, start_j,
            end_i, end_j, mat):
 
    # Stores the number of 1s
    # present in current submatrix
    count = 0
 
    # Traverse the submatrix
    for x in range(start_i, end_i):
        for y in range(start_j, end_j):
 
            # If mat[x][y] is equal to 1
            if (mat[x][y] == 1):
 
                # Increase count by 1
                count += 1
 
    # Return the total count of 1s
    return count
 
# Function to find the minimum number of 1s
# present in a sub-matrix of size A * B or B * A
def findMinimumCount(N, M, A, B, mat):
 
    # Stores the minimum count of 1s
    minimum = 1e9
 
    # Iterate i from 0 to N
    for i in range(N):
 
        # Iterate j from 0 to M
        for j in range(M):
 
            # If a valid sub matrix of size
            # A * B from (i, j) is possible
            if (i + A <= N and j + B <= M):
 
                # Count the number of 1s
                # present in the sub matrix
                # of size A * B from (i, j)
                count = count1s(i, j, i + A, j + B, mat)
 
                # Update minimum if count is
                # less than the current minimum
                minimum = min(count, minimum)
 
            # If a valid sub matrix of size
            # B * A from (i, j) is possible
            if (i + B <= N and j + A <= M):
 
                # Count the number of 1s in the
                # sub matrix of size B * A from (i, j)
                count = count1s(i, j, i + B, j + A, mat)
 
                # Update minimum if count is
                # less than the current minimum
                minimum = min(count, minimum)
 
    # Return minimum as the final result
    return minimum
 
# Driver Code
if __name__ == "__main__":
    # Given Input
    A = 2
    B = 2
    N = 3
    M = 4
    mat = [[1, 0, 1, 0],
           [0, 1, 0, 1],
           [1, 0, 1, 0]]
 
    # Function call to find the minimum number
    # of 1s in a submatrix of size A * B or B * A
    print(findMinimumCount(N, M, A, B, mat))
 
    # This code is contributed by ukasp.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
    
// Function to count number of 1s
// present in a sub matrix from
// (start_i, start_j) to (end_i, end_j)
static int count1s(int start_i, int start_j,
                   int end_i, int end_j,
                   List> mat)
{
     
    // Stores the number of 1s
    // present in current submatrix
    int count = 0;
 
    // Traverse the submatrix
    for(int x = start_i; x < end_i; x++)
    {
        for(int y = start_j; y < end_j; y++)
        {
             
            // If mat[x][y] is equal to 1
            if (mat[x][y] == 1)
 
                // Increase count by 1
                count++;
        }
    }
 
    // Return the total count of 1s
    return count;
}
 
// Function to find the minimum number of 1s
// present in a sub-matrix of size A * B or B * A
static void findMinimumCount(int N, int M, int A, int B,
                             List> mat)
{
     
    // Stores the minimum count of 1s
    int minimum = 1000000;
 
    // Iterate i from 0 to N
    for(int i = 0; i < N; i++)
    {
 
        // Iterate j from 0 to M
        for(int j = 0; j < M; j++)
        {
             
            // If a valid sub matrix of size
            // A * B from (i, j) is possible
            if ((i + A <= N) && (j + B <= M))
            {
                 
                // Count the number of 1s
                // present in the sub matrix
                // of size A * B from (i, j)
                int count = count1s(i, j, i + A,
                                    j + B, mat);
 
                // Update minimum if count is
                // less than the current minimum
                minimum = Math.Min(count, minimum);
            }
 
            // If a valid sub matrix of size
            // B * A from (i, j) is possible
            if ((i + B <= N) && (j + A <= M))
            {
                 
                // Count the number of 1s in the
                // sub matrix of size B * A from (i, j)
                int count = count1s(i, j, i + B,
                                    j + A, mat);
 
                // Update minimum if count is
                // less than the current minimum
                minimum = Math.Min(count, minimum);
            }
        }
    }
 
    // Return minimum as the final result
    Console.WriteLine(minimum);
}
 
// Driver Code
public static void Main()
{
     
    // Given Input
    int A = 2, B = 2;
    int N = 3, M = 4;
     
    List> mat = new List>();
    mat.Add(new List(new int[]{1, 0, 1, 0}));
    mat.Add(new List(new int[]{0, 1, 0, 1}));
    mat.Add(new List(new int[]{1, 0, 1, 0}));
 
    // Function call to find the minimum number
    // of 1s in a submatrix of size A * B or B * A
    findMinimumCount(N, M, A, B, mat);
}
}
 
// This code is contributed by ipg2016107


Javascript


输出:
2

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live