📌  相关文章
📜  检查给定的二进制矩阵中是否有 T 个连续的 0 块

📅  最后修改于: 2022-05-13 01:56:08.121000             🧑  作者: Mango

检查给定的二进制矩阵中是否有 T 个连续的 0 块

给定一个维度为M*N的二进制矩阵mat[][] ,任务是检查是否存在T个连续的0块以及至少2*max(M, N) 个值为0的单元。如果发现是真的,则打印Yes 。否则,打印No

例子:

方法:这个想法是计算值为0的单元格的数量,如果满足条件,则找到MN的最大公约数并执行深度优先搜索以找到值为0的连接组件的数量。按照解决问题的步骤如下:

  • 初始化一个变量,比如黑色0并计算值为0的单元格的数量。
  • 如果黑色小于等于2*max(M, N)则打印No并从函数返回。
  • 初始化一个变量,比如blackSpots0来计算连续点的数量。
  • 使用变量 i 迭代范围[0, M)并使用变量j嵌套迭代范围[0, N)并且如果当前单元格访问 [i][j]false并且如果mat[i][j ]0 ,然后使用当前单元格对给定矩阵执行 DFS 遍历,以在行、列和对角方向上找到值为0的连续段,并将blackSpots的值增加1
  • 执行上述步骤后,如果blackSpots的值大于gcd(M, N) ,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
const long long M = 1e9 + 7;
 
// Stores the moves in the matrix
vector > directions
    = { { 0, 1 }, { -1, 0 }, { 0, -1 },
        { 1, 0 }, { 1, 1 }, { -1, -1 },
        { -1, 1 }, { 1, -1 } };
 
// Function to find if the current cell
// lies in the matrix or not
bool isInside(int i, int j, int N, int M)
{
    if (i >= 0 && i < N
        && j >= 0 && j < M) {
        return true;
    }
    return false;
}
 
// Function to perform the DFS Traversal
void DFS(vector > mat, int N,
         int M, int i, int j,
         vector >& visited)
{
    if (visited[i][j] == true) {
 
        return;
    }
 
    visited[i][j] = true;
 
    // Iterate over the direction vector
    for (auto it : directions) {
        int I = i + it.first;
        int J = j + it.second;
        if (isInside(I, J, N, M)) {
            if (mat[I][J] == 0) {
 
                // DFS Call
                DFS(mat, N, M, I,
                    J, visited);
            }
        }
    }
}
 
// Function to check if it satisfy the
// given criteria or not
void check(int N, int M,
           vector >& mat)
{
    // Keeps the count of cell having
    // value as 0
    int black = 0;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (mat[i][j] == 0) {
                black++;
            }
        }
    }
 
    // If condition doesn't satisfy
    if (black < 2 * (max(N, M))) {
        cout << "NO" << endl;
        return;
    }
 
    vector > visited;
 
    for (int i = 0; i < N; i++) {
        vector temp;
        for (int j = 0; j < M; j++) {
            temp.push_back(false);
        }
        visited.push_back(temp);
    }
 
    // Keeps the track of unvisted cell
    // having values 0
    int black_spots = 0;
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (visited[i][j] == false
                && mat[i][j] == 0) {
 
                // Increasing count of
                // black_spot
                black_spots++;
                DFS(mat, N, M, i, j, visited);
            }
        }
    }
 
    // Find the GCD of N and M
    int T = __gcd(N, M);
 
    // Print the result
    cout << (black_spots >= T ? "Yes" : "No");
}
 
// Driver Code
int main()
{
    int N = 3, M = 3;
    vector > mat
        = { { 0, 0, 1 }, { 1, 1, 1 }, { 0, 0, 1 } };
 
    check(M, N, mat);
 
    return 0;
}


Python3
# python program for the above approach
 
import math
 
M = 1000000000 + 7
 
# Stores the moves in the matrix
directions = [[0, 1], [-1, 0], [0, -1],
              [1, 0], [1, 1], [-1, -1],
              [-1, 1], [1, -1]]
 
# Function to find if the current cell
# lies in the matrix or not
 
 
def isInside(i, j, N, M):
 
    if (i >= 0 and i < N and j >= 0 and j < M):
        return True
 
    return False
 
 
# Function to perform the DFS Traversal
def DFS(mat, N, M, i, j, visited):
 
    if (visited[i][j] == True):
 
        return
 
    visited[i][j] = True
 
    # Iterate over the direction vector
    for it in directions:
        I = i + it[0]
        J = j + it[1]
        if (isInside(I, J, N, M)):
            if (mat[I][J] == 0):
 
                # DFS Call
                DFS(mat, N, M, I, J, visited)
 
 
# Function to check if it satisfy the
# given criteria or not
def check(N, M, mat):
 
    # Keeps the count of cell having
    # value as 0
    black = 0
 
    for i in range(0, N):
        for j in range(0, M):
            if (mat[i][j] == 0):
                black += 1
 
        # If condition doesn't satisfy
    if (black < 2 * (max(N, M))):
        print("NO")
        return
 
    visited = []
    for i in range(0, N):
        temp = []
        for j in range(0, M):
            temp.append(False)
 
        visited.append(temp)
 
        # Keeps the track of unvisted cell
        # having values 0
    black_spots = 0
 
    for i in range(0, N):
        for j in range(0, M):
            if (visited[i][j] == False and mat[i][j] == 0):
 
                # Increasing count of
                # black_spot
                black_spots += 1
                DFS(mat, N, M, i, j, visited)
 
        # Find the GCD of N and M
    T = math.gcd(N, M)
 
    # Print the result
    if black_spots >= T:
        print("Yes")
    else:
        print("No")
 
 
# Driver Code
if __name__ == "__main__":
 
    N = 3
    M = 3
    mat = [[0, 0, 1], [1, 1, 1], [0, 0, 1]]
 
    check(M, N, mat)
 
    # This code is contributed by rakeshsahni


Javascript



输出:
NO

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