📜  全部为1的平方矩阵数

📅  最后修改于: 2021-05-04 12:43:50             🧑  作者: 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


输出 :

7

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