📜  查询以查找矩阵中给定大小的连接网格组件的数量

📅  最后修改于: 2021-10-25 04:59:30             🧑  作者: Mango

给定一个只包含0 s 和1 s 的矩阵 mat[][]和一个数组queries[] ,任务是针对每个查询,比如说k ,是找到连接的网格组件(由 1 s 组成的单元格)的数量大小为k
注意:如果两个像元在上、下、左、右而非对角线方向上共享一条边,则它们是连接的。

例子:

方法:想法是在网格中使用 BFS/DFS 计算和存储无序映射中各种大小的连通分量的数量的频率,然后我们可以迭代查询数组并为每个分配连通分量的计数数组中给定的大小。
以下是解决问题的步骤:

  • 遍历矩阵并从包含“1”的未访问单元格执行 BFS
  • 检查未访问的有效相邻单元是否包含 1 并将这些单元推入队列。
  • 对所有未访问的单元格中包含 1 的单元格重复上述两个步骤。
  • 打印具有查询中每个给定大小的连接组件数量的数组。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
#include 
using namespace std;
  
const int n = 6;
const int m = 6;
  
const int dx[] = { 0, 1, -1, 0 };
const int dy[] = { 1, 0, 0, -1 };
  
// stores information about  which cell
// are already visited in a particular BFS
int visited[n][m];
  
// Stores the final result grid
int result[n][m];
  
// Stores the count of cells in
// the largest connected component
int COUNT;
  
// Function checks if a cell is valid, i.e.
// it is inside the grid and equal to 1
bool is_valid(int x, int y, int matrix[n][m])
{
    if (x < n && y < m && x >= 0 && y >= 0) {
        if (visited[x][y] == false
            && matrix[x][y] == 1)
            return true;
        else
            return false;
    }
    else
        return false;
}
  
// Map to count the frequency of
// each connected component
map mp;
  
// function to calculate the
// largest connected component
void findComponentSize(int matrix[n][m])
{
    // Iterate over every cell
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
  
            if (!visited[i][j] && matrix[i][j] == 1) {
                COUNT = 0;
  
                // Stores the indices of the matrix cells
                queue > q;
  
                // Mark the starting cell as visited
                // and push it into the queue
                q.push({ i, j });
                visited[i][j] = true;
  
                // Iterate while the queue
                // is not empty
                while (!q.empty()) {
  
                    pair p = q.front();
                    q.pop();
                    int x = p.first, y = p.second;
                    COUNT++;
  
                    // Go to the adjacent cells
                    for (int i = 0; i < 4; i++) {
  
                        int newX = x + dx[i];
                        int newY = y + dy[i];
  
                        if (is_valid(newX, newY, matrix)) {
                            q.push({ newX, newY });
                            visited[newX][newY] = true;
                        }
                    }
                }
  
                mp[COUNT]++;
            }
        }
    }
}
// Drivers Code
int main()
{
    // Given input array of 1s and 0s
    int matrix[n][m]
        = { { 1, 1, 1, 1, 1, 1 }, { 1, 1, 0, 0, 0, 0 }, 
            { 0, 0, 0, 1, 1, 1 }, { 0, 0, 0, 1, 1, 1 }, 
            { 0, 0, 1, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0 } };
  
    // queries array
    int queries[] = { 6, 1, 8, 2 };
  
    // sizeof queries array
    int N = sizeof(queries) / sizeof(queries[0]);
  
    // Initialize all cells unvisited
    memset(visited, false, sizeof visited);
  
    // Count the frequency of each connected component
    findComponentSize(matrix);
  
    // Iterate over the given queries array and
    // And answer the queries
    for (int i = 0; i < N; i++)
        cout << mp[queries[i]] << " ";
  
    return 0;
}


输出:
1 2 1 0

时间复杂度: O(n*m)
空间复杂度: O(n*m)

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