📜  全为 1 的方阵数

📅  最后修改于: 2021-09-17 16:05:26             🧑  作者: Mango

给定一个只包含 0 和 1 的N*M矩阵,任务是计算包含全 1 的方形子矩阵的数量。
例子:

方法:这个问题可以用动态规划解决。

  1. 让数组arr[i][j]存储以(i, j)结尾的方阵的个数
  2. 求以(i, j)结尾的平方数的递推关系可以由下式给出:
    • 如果 arr[i][j] 是 1:
      • arr[i][j] = min( min(arr[i-1][j], arr[i][j-1]), arr[i-1][j-1]) + 1
    • 否则,如果 arr[i][j] 为 0:
      • arr[i][j] = 0
  3. 计算数组的总和,它等于全 1 的平方子矩阵的数量。

下面是上述方法的实现:

CPP
// C++ program to return the number of
// square submatrices with all 1s
#include 
using namespace std;
 
#define n 3
#define m 3
 
// Function to return the number of
// square submatrices with all 1s
int countSquareMatrices(int a[][m],
                        int N, int M)
{
    // Initialize count variable
    int count = 0;
 
    for (int i = 1; i < N; i++) {
        for (int j = 1; j < M; j++) {
            // If a[i][j] is equal to 0
            if (a[i][j] == 0)
                continue;
 
            // Calculate number of
            // square submatrices
            // ending at (i, j)
            a[i][j] = min(min(a[i - 1][j],
                              a[i][j - 1]),
                          a[i - 1][j - 1])
                      + 1;
        }
    }
 
    // Calculate the sum of the array
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++)
            count += a[i][j];
 
    return count;
}
 
// Driver code
int main()
{
    int arr[][m] = { { 1, 0, 1 },
                     { 1, 1, 0 },
                     { 1, 1, 0 } };
 
    cout << countSquareMatrices(arr, n, m);
 
    return 0;
}


Java
// Java program to return the number of
// square submatrices with all 1s
class GFG
{
     
    final static int n = 3;
    final static int m = 3;
     
    // Function to return the number of
    // square submatrices with all 1s
    static int countSquareMatrices(int a[][], int N, int M)
    {
        // Initialize count variable
        int count = 0;
     
        for (int i = 1; i < N; i++)
        {
            for (int j = 1; j < M; j++)
            {
                // If a[i][j] is equal to 0
                if (a[i][j] == 0)
                    continue;
     
                // Calculate number of
                // square submatrices
                // ending at (i, j)
                a[i][j] = Math.min(Math.min(a[i - 1][j], a[i][j - 1]),
                            a[i - 1][j - 1]) + 1;
            }
        }
     
        // Calculate the sum of the array
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
                count += a[i][j];
     
        return count;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[][] = { { 1, 0, 1 },
                        { 1, 1, 0 },
                        { 1, 1, 0 } };
     
        System.out.println(countSquareMatrices(arr, n, m));
    }
}
 
// This code is contributed by AnkitRai01


Python
# Python3 program to return the number of
# square submatrices with all 1s
n = 3
m = 3
 
# Function to return the number of
# square submatrices with all 1s
def countSquareMatrices(a, N, M):
     
    # Initialize count variable
    count = 0
 
    for i in range(1, N):
        for j in range(1, M):
             
            # If a[i][j] is equal to 0
            if (a[i][j] == 0):
                continue
 
            # Calculate number of
            # square submatrices
            # ending at (i, j)
            a[i][j] = min([a[i - 1][j],
                      a[i][j - 1], a[i - 1][j - 1]])+1
 
    # Calculate the sum of the array
    for i in range(N):
        for j in range(M):
            count += a[i][j]
 
    return count
 
# Driver code
 
arr = [ [ 1, 0, 1],
    [ 1, 1, 0 ],
    [ 1, 1, 0 ] ]
 
print(countSquareMatrices(arr, n, m))
 
# This code is contributed by mohit kumar 29


C#
// C# program to return the number of
// square submatrices with all 1s
using System;
 
class GFG
{
     
    static int n = 3;
    static int m = 3;
     
    // Function to return the number of
    // square submatrices with all 1s
    static int countSquareMatrices(int [,]a, int N, int M)
    {
        // Initialize count variable
        int count = 0;
     
        for (int i = 1; i < N; i++)
        {
            for (int j = 1; j < M; j++)
            {
                // If a[i][j] is equal to 0
                if (a[i, j] == 0)
                    continue;
     
                // Calculate number of
                // square submatrices
                // ending at (i, j)
                a[i, j] = Math.Min(Math.Min(a[i - 1, j], a[i, j - 1]),
                            a[i - 1, j - 1]) + 1;
            }
        }
     
        // Calculate the sum of the array
        for (int i = 0; i < N; i++)
            for (int j = 0; j < M; j++)
                count += a[i, j];
     
        return count;
    }
     
    // Driver code
    public static void Main()
    {
        int [,]arr = { { 1, 0, 1 },
                        { 1, 1, 0 },
                        { 1, 1, 0 } };
     
        Console.WriteLine(countSquareMatrices(arr, n, m));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


输出 :

7

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程