📜  查找矩阵中的空腔数

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

查找矩阵中的空腔数

计算二维矩阵中空洞的数量,空洞被定义为所有周围的数字都大于中间的数字。
例子:

来源:Ola 面试体验集 13
下面是上述方法的实现。

C++
// C++ program find number of cavities in a matrix
#include 
using namespace std;
 
const int MAX = 100;
 
int countCavities(int a[][MAX], int n)
{
    int A[n + 2][n + 2];
    int coun = 0;
 
    // form another matrix with one extra layer of
    // boundary elements.
    // Boundary elements will contain max value.
    for (int i = 0; i < n + 2; i++) {
        for (int j = 0; j < n + 2; j++) {
            if ((i == 0) || (j == 0) || (i == n + 1) ||
                                         (j == n + 1))
                A[i][j] = INT_MAX;
            else
                A[i][j] = a[i - 1][j - 1];
        }
    }
 
    // Check for cavities in the modified matrix
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
 
            // check for all  directions
            if ((A[i][j] < A[i - 1][j]) && (A[i][j] < A[i + 1][j]) &&
                (A[i][j] < A[i][j - 1]) && (A[i][j] < A[i][j + 1]) &&
                (A[i][j] < A[i - 1][j - 1]) && (A[i][j] < A[i + 1][j + 1]) &&
                (A[i][j] < A[i - 1][j + 1]) && (A[i][j] < A[i + 1][j - 1]))
                coun++;
        }
    }
 
    return coun;
}
 
int main()
{
    int a[][MAX] = { { 4, 5, 6 }, { 7, 1, 5 }, { 4, 5, 6 } };
    int n = 3;
    cout << countCavities(a, n);
    return 0;
}


Java
// Java program find number of cavities in a matrix
class GfG {
 
static int MAX = 100;
 
static int countCavities(int a[][], int n)
{
    int A[][] = new int[n + 2][n + 2];
    int coun = 0;
 
    // form another matrix with one extra layer of
    // boundary elements.
    // Boundary elements will contain max value.
    for (int i = 0; i < n + 2; i++) {
        for (int j = 0; j < n + 2; j++) {
            if ((i == 0) || (j == 0) || (i == n + 1) || (j == n + 1))
                A[i][j] = Integer.MAX_VALUE;
            else
                A[i][j] = a[i - 1][j - 1];
        }
    }
 
    // Check for cavities in the modified matrix
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
 
            // check for all directions
            if ((A[i][j] < A[i - 1][j]) && (A[i][j] < A[i + 1][j]) &&
                (A[i][j] < A[i][j - 1]) && (A[i][j] < A[i][j + 1]) &&
                (A[i][j] < A[i - 1][j - 1]) && (A[i][j] < A[i + 1][j + 1]) &&
                (A[i][j] < A[i - 1][j + 1]) && (A[i][j] < A[i + 1][j - 1]))
                coun++;
        }
    }
 
    return coun;
}
 
public static void main(String[] args)
{
    int a[][] = new int[][]{{ 4, 5, 6 }, { 7, 1, 5 }, { 4, 5, 6 }};
    int n = 3;
System.out.println(countCavities(a, n));
}
}


C#
// C# program find number of cavities in a matrix
using System;
 
class GfG
{
 
    static int MAX = 100;
 
    static int countCavities(int [,]a, int n)
    {
        int [,]A = new int[n + 2, n + 2];
        int coun = 0;
 
        // form another matrix with one extra layer of
        // boundary elements.
        // Boundary elements will contain max value.
        for (int i = 0; i < n + 2; i++)
        {
            for (int j = 0; j < n + 2; j++)
            {
                if ((i == 0) || (j == 0) || (i == n + 1) || (j == n + 1))
                    A[i, j] = int.MaxValue;
                else
                    A[i, j] = a[i - 1, j - 1];
            }
        }
 
        // Check for cavities in the modified matrix
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
 
                // check for all directions
                if ((A[i, j] < A[i - 1, j]) && (A[i, j] < A[i + 1, j]) &&
                    (A[i, j] < A[i, j - 1]) && (A[i, j] < A[i, j + 1]) &&
                    (A[i, j] < A[i - 1, j - 1]) && (A[i, j] < A[i + 1, j + 1]) &&
                    (A[i, j] < A[i - 1, j + 1]) && (A[i, j] < A[i + 1, j - 1]))
                    coun++;
            }
        }
        return coun;
    }
 
    public static void Main(String[] args)
    {
        int [,]a = new int[,]{{ 4, 5, 6 }, { 7, 1, 5 }, { 4, 5, 6 }};
        int n = 3;
        Console.WriteLine(countCavities(a, n));
    }
}
 
// This code contributed by Rajput-Ji


PHP


Javascript


输出:

1

优化我们可以通过以下步骤避免使用额外的空间和额外的条件。
1) 显式检查四个角元素,第一行、最后一行、第一列和最后一列的剩余元素。
2)使用上述逻辑检查剩余元素。

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