📜  查找可以在具有阻塞区域的矩阵中建立的区域Z的平方数

📅  最后修改于: 2021-05-05 01:34:43             🧑  作者: Mango

N * N矩阵中,某些行和列被阻塞。找到可以在给定约束的这种矩阵中构建的区域Z的平方数:

  1. 每个像元将贡献1个单位面积,而1个像元恰好属于1个子网格。
  2. 所有单元格应为畅通的单元格。

被阻塞的行和列由两个数组row []col []表示

例子:

方法:

  • 在这里,实际矩阵将被划分为几个矩形。只能在这些矩形中构建区域。假设存在一个尺寸为R * L的矩形,则区域Z的区域数将等于(R / K)*(L / K) ,其中KZ的平方根,因为尺寸K的最大平方可以拟合长度LL / K ,宽度相同。因此,任务是找到所有此类矩形的尺寸,并计算其中可能的区域数量。
  • 为了找到矩形,首先,所有被阻塞的行和列均已按排序顺序给出。现在,考虑第一阻塞行和第一阻塞列之间的区域。该区域完全畅通无阻。类似地,任意两个连续的受阻列与任意两个连续的受阻行之间的区域将形成一个非阻塞区域。
  • 因此,基本上任何未阻塞的区域都将位于任何两个连续的阻塞行和任何两个连续的阻塞列之间。我们可以存储连续的阻塞行和连续的阻塞列之间的长度,然后我们可以从行数组取任意长度,从列数组取任意长度并将它们相乘。
  • 找到所有这些不受阻碍的区域,并求和可能合计的大小为K * K的正方形总数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to calculate the number of
// square areas of size K*K
int subgrids(int N, int Z, int row[],
             int col[], int r, int c)
{
    // Row array and column array to
    // store the lengths of differences
    // between consecutive rows/columns
    vector conrow;
    vector concol;
  
    int K = sqrt(Z);
  
    // Fill the conrow vector
    conrow.push_back(row[0] - 0 - 1);
    conrow.push_back(N + 1 - row[r - 1] - 1);
    for (int i = 1; i < r; i++) {
        conrow.push_back(row[i] - row[i - 1] - 1);
    }
  
    // Fill the concol vector
    concol.push_back(col[0] - 0 - 1);
    concol.push_back(N + 1 - col - 1);
    for (int i = 1; i < c; i++) {
        concol.push_back(col[i] - col[i - 1] - 1);
    }
  
    int row_size = conrow.size();
    int col_size = concol.size();
  
    // To store the required answer
    int answer = 0;
  
    // Every pair of row size and column size
    // would result in an unblocked region
    for (int i = 0; i < row_size; i++) {
        for (int j = 0; j < col_size; j++) {
            int total = (concol[j] / K)
                        * (conrow[i] / K);
            answer += (total);
        }
    }
  
    return answer;
}
  
// Driver code
int main()
{
    int N = 8, Z = 4;
    int row[] = { 4, 6 };
    int col[] = { 3, 8 };
    int r = sizeof(row) / sizeof(row[0]);
    int c = sizeof(col) / sizeof(col[0]);
  
    cout << subgrids(N, Z, row, col, r, c);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
  
// Function to calculate the number of
// square areas of size K*K
static int subgrids(int N, int Z, int row[],
                    int col[], int r, int d)
{
    // Row array and column array to
    // store the lengths of differences
    // between consecutive rows/columns
    Vector conrow = new Vector();
    Vector concol = new Vector();
  
    int K = (int) Math.sqrt(Z);
  
    // Fill the conrow vector
    conrow.add(row[0] - 0 - 1);
    conrow.add(N + 1 - row[r - 1] - 1);
    for (int i = 1; i < r; i++)
    {
        conrow.add(row[i] - row[i - 1] - 1);
    }
  
    // Fill the concol vector
    concol.add(col[0] - 0 - 1);
    concol.add(N + 1 - col[d - 1] - 1);
    for (int i = 1; i < d; i++)
    {
        concol.add(col[i] - col[i - 1] - 1);
    }
  
    int row_size = conrow.size();
    int col_size = concol.size();
  
    // To store the required answer
    int answer = 0;
  
    // Every pair of row size and column size
    // would result in an unblocked region
    for (int i = 0; i < row_size; i++)
    {
        for (int j = 0; j < col_size; j++)
        {
            int total = (concol.get(j) / K) * 
                        (conrow.get(i) / K);
            answer += (total);
        }
    }
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 8, Z = 4;
    int row[] = { 4, 6 };
    int col[] = { 3, 8 };
    int r = row.length;
    int d = col.length;
  
    System.out.print(subgrids(N, Z, row, col, r, d));
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation of the approach 
from math import sqrt
  
# Function to calculate the number of 
# square areas of size K*K 
def subgrids(N, Z, row, col, r, d) :
      
    # Row array and column array to
    # store the lengths of differences
    # between consecutive rows/columns
    conrow = [];
    concol = [];
      
    K = int(sqrt(Z));
      
    # Fill the conrow vector
    conrow.append(row[0] - 0 - 1)
    conrow.append(N + 1 - row[r - 1] - 1)
      
    for i in range(1, r) :
        conrow.append(row[i] - row[i - 1] - 1);
          
    # Fill the concol vector
    concol.append(col[0] - 0 - 1)
    concol.append(N + 1 - col[d - 1] - 1)
      
    for i in range(1, d) :
        concol.append(col[i] - col[i - 1] - 1);
          
    row_size = len(conrow)
    col_size = len(concol)
  
    # To store the required answer 
    answer = 0
      
    # Every pair of row size and column size
    # would result in an unblocked region
    for i in range(row_size) :
        for j in range(col_size) :
            total = (concol[j] // K) * \
                    (conrow[i] // K)
            answer += (total)
              
    return answer
  
# Driver code 
if __name__ == "__main__" : 
  
    N = 8; Z = 4
    row = [ 4, 6 ]
    col = [ 3, 8 ] 
    r = len(row)
    d = len(col) 
  
    print(subgrids(N, Z, row, col, r, d))
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Function to calculate the number of
// square areas of size K*K
static int subgrids(int N, int Z, int []row,
                    int []col, int r, int d)
{
    // Row array and column array to
    // store the lengths of differences
    // between consecutive rows/columns
    List conrow = new List();
    List concol = new List();
  
    int K = (int) Math.Sqrt(Z);
  
    // Fill the conrow vector
    conrow.Add(row[0] - 0 - 1);
    conrow.Add(N + 1 - row[r - 1] - 1);
    for (int i = 1; i < r; i++)
    {
        conrow.Add(row[i] - row[i - 1] - 1);
    }
  
    // Fill the concol vector
    concol.Add(col[0] - 0 - 1);
    concol.Add(N + 1 - col[d - 1] - 1);
    for (int i = 1; i < d; i++)
    {
        concol.Add(col[i] - col[i - 1] - 1);
    }
  
    int row_size = conrow.Count;
    int col_size = concol.Count;
  
    // To store the required answer
    int answer = 0;
  
    // Every pair of row size and column size
    // would result in an unblocked region
    for (int i = 0; i < row_size; i++)
    {
        for (int j = 0; j < col_size; j++)
        {
            int total = (concol[j] / K) * 
                        (conrow[i] / K);
            answer += (total);
        }
    }
    return answer;
}
  
// Driver code
public static void Main(String[] args)
{
    int N = 8, Z = 4;
    int []row = { 4, 6 };
    int []col = { 3, 8 };
    int r = row.Length;
    int d = col.Length;
  
    Console.Write(subgrids(N, Z, row, col, r, d));
}
}
  
// This code is contributed by Rajput-Ji


输出:
6