📌  相关文章
📜  查找由二进制矩阵中的所有 1 形成的最大“+”的大小

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

查找由二进制矩阵中的所有 1 形成的最大“+”的大小

给定一个 NXN 二进制矩阵,找出由所有 1 组成的最大“+”的大小。
例子:

对于上述矩阵,最大的“+”将由大小为 17 的突出显示部分形成。

这个想法是维护四个辅助矩阵 left[][], right[][], top[][], bottom[][] 来存储每个方向上的连续 1。对于输入矩阵中的每个单元格 (i, j),我们将以下信息存储在这四个矩阵中:

left(i, j) stores maximum number of
consecutive 1's to the left of cell (i, j) 
including cell (i, j).

right(i, j) stores maximum number of
consecutive 1's to the right of cell (i, j) 
including cell (i, j).

top(i, j) stores maximum number of
consecutive 1's at top of cell (i, j) 
including cell (i, j).

bottom(i, j) stores maximum number of
consecutive 1's at bottom of cell (i, j) 
including cell (i, j).

在计算上述矩阵的每个单元格的值之后,最大的 + 将由输入矩阵的一个单元格形成,该单元格通过考虑 (left(i, j), right(i, j), top(i, j) 的最小值), 底部(i, j) )
我们可以使用动态规划来计算每个方向上连续 1 的总数。

if mat(i, j) == 1
    left(i, j) = left(i, j - 1) + 1
else left(i, j) = 0

if mat(i, j) == 1
    top(i, j) = top(i - 1, j) + 1;
else
    top(i, j) = 0;

if mat(i, j) == 1
    bottom(i, j) = bottom(i + 1, j) + 1;
else
    bottom(i, j) = 0;    

if mat(i, j) == 1
    right(i, j) = right(i, j + 1) + 1;
else
    right(i, j) = 0;

下面是上述想法的实现——

C++
// C++ program to find the size of the largest '+'
// formed by all 1's in given binary matrix
#include 
using namespace std;
 
// size of binary square matrix
#define N 10
 
// Function to find the size of the largest '+'
// formed by all 1's in given binary matrix
int findLargestPlus(int mat[N][N])
{
    // left[j][j], right[i][j], top[i][j] and
    // bottom[i][j] store maximum number of
    // consecutive 1's present to the left,
    // right, top and bottom of mat[i][j] including
    // cell(i, j) respectively
    int left[N][N], right[N][N], top[N][N],
        bottom[N][N];
 
    // initialize above four matrix
    for (int i = 0; i < N; i++)
    {
        // initialize first row of top
        top[0][i] = mat[0][i];
 
        // initialize last row of bottom
        bottom[N - 1][i] = mat[N - 1][i];
 
        // initialize first column of left
        left[i][0] = mat[i][0];
 
        // initialize last column of right
        right[i][N - 1] = mat[i][N - 1];
    }
 
    // fill all cells of above four matrix
    for (int i = 0; i < N; i++)
    {
        for (int j = 1; j < N; j++)
        {
            // calculate left matrix (filled left to right)
            if (mat[i][j] == 1)
                left[i][j] = left[i][j - 1] + 1;
            else
                left[i][j] = 0;
 
            // calculate top matrix
            if (mat[j][i] == 1)
                top[j][i] = top[j - 1][i] + 1;
            else
                top[j][i] = 0;
 
            // calculate new value of j to calculate
            // value of bottom(i, j) and right(i, j)
            j = N - 1 - j;
 
            // calculate bottom matrix
            if (mat[j][i] == 1)
                bottom[j][i] = bottom[j + 1][i] + 1;
            else
                bottom[j][i] = 0;
 
            // calculate right matrix
            if (mat[i][j] == 1)
                right[i][j] = right[i][j + 1] + 1;
            else
                right[i][j] = 0;
 
            // revert back to old j
            j = N - 1 - j;
        }
    }
 
    // n stores length of longest + found so far
    int n = 0;
 
    // compute longest +
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            // find minimum of left(i, j), right(i, j),
            // top(i, j), bottom(i, j)
            int len = min(min(top[i][j], bottom[i][j]),
                          min(left[i][j], right[i][j]));
 
            // largest + would be formed by a cell that
            // has maximum value
            if(len > n)
                n = len;
        }
    }
 
    // 4 directions of length n - 1 and 1 for middle cell
    if (n)
       return 4 * (n - 1) + 1;
 
    // matrix contains all 0's
    return 0;
}
 
/* Driver function to test above functions */
int main()
{
    // Binary Matrix of size N
    int mat[N][N] =
    {
        { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
        { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
        { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
        { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },
        { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
        { 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
        { 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 },
        { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 },
        { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }
    };
 
    cout << findLargestPlus(mat);
 
    return 0;
}


Java
// Java program to find the size of the largest '+'
// formed by all 1's in given binary matrix
 
import java.io.*;
 
class GFG {
     
    // size of binary square matrix
    static int N = 10;
 
    // Function to find the size of the largest '+'
    // formed by all 1's in given binary matrix
    static int findLargestPlus(int mat[][])
    {
         
        // left[j][j], right[i][j], top[i][j] and
        // bottom[i][j] store maximum number of
        // consecutive 1's present to the left,
        // right, top and bottom of mat[i][j]
        // including cell(i, j) respectively
        int left[][] = new int[N][N];
        int right[][] = new int[N][N];
        int top[][] = new int[N][N];
        int bottom[][] = new int[N][N];
 
        // initialize above four matrix
        for (int i = 0; i < N; i++) {
             
            // initialize first row of top
            top[0][i] = mat[0][i];
 
            // initialize last row of bottom
            bottom[N - 1][i] = mat[N - 1][i];
 
            // initialize first column of left
            left[i][0] = mat[i][0];
 
            // initialize last column of right
            right[i][N - 1] = mat[i][N - 1];
        }
 
        // fill all cells of above four matrix
        for (int i = 0; i < N; i++) {
            for (int j = 1; j < N; j++) {
                 
                // calculate left matrix
                // (filled left to right)
                if (mat[i][j] == 1)
                    left[i][j] = left[i][j - 1] + 1;
                else
                    left[i][j] = 0;
 
                // calculate top matrix
                if (mat[j][i] == 1)
                    top[j][i] = top[j - 1][i] + 1;
                else
                    top[j][i] = 0;
 
                // calculate new value of j to
                // calculate value of bottom(i, j)
                // and right(i, j)
                j = N - 1 - j;
 
                // calculate bottom matrix
                if (mat[j][i] == 1)
                    bottom[j][i] = bottom[j + 1][i] + 1;
                else
                    bottom[j][i] = 0;
 
                // calculate right matrix
                if (mat[i][j] == 1)
                    right[i][j] = right[i][j + 1] + 1;
                else
                    right[i][j] = 0;
 
                // revert back to old j
                j = N - 1 - j;
            }
        }
 
        // n stores length of longest + found so far
        int n = 0;
 
        // compute longest +
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                // find minimum of left(i, j),
                // right(i, j), top(i, j),
                // bottom(i, j)
                int len = Math.min(Math.min(top[i][j],
                    bottom[i][j]),Math.min(left[i][j],
                                        right[i][j]));
 
                // largest + would be formed by a
                // cell that has maximum value
                if (len > n)
                    n = len;
            }
        }
 
        // 4 directions of length n - 1 and 1 for
        // middle cell
        if (n > 0)
            return 4 * (n - 1) + 1;
 
        // matrix contains all 0's
        return 0;
    }
 
    /* Driver function to test above functions */
    public static void main(String[] args)
    {
         
        // Binary Matrix of size N
        int mat[][] = {
            { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
            { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
            { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
            { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },
            { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
            { 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 },
            { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 },
            { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }
        };
        System.out.println(findLargestPlus(mat));
    }
}
 
// This code is contributed by vt_m.


Python 3
# Python 3 program to find the size
# of the largest '+' formed by all
# 1's in given binary matrix
 
# size of binary square matrix
N = 10
 
# Function to find the size of the
# largest '+' formed by all 1's in
# given binary matrix
def findLargestPlus(mat):
 
    # left[j][j], right[i][j], top[i][j] and
    # bottom[i][j] store maximum number of
    # consecutive 1's present to the left,
    # right, top and bottom of mat[i][j] including
    # cell(i, j) respectively
    left = [[0 for x in range(N)]
               for y in range(N)]
    right = [[0 for x in range(N)]
                for y in range(N)]
    top = [[0 for x in range(N)]
              for y in range(N)]
    bottom = [[0 for x in range(N)]
                 for y in range(N)]
 
    # initialize above four matrix
    for i in range(N):
         
        # initialize first row of top
        top[0][i] = mat[0][i]
 
        # initialize last row of bottom
        bottom[N - 1][i] = mat[N - 1][i]
 
        # initialize first column of left
        left[i][0] = mat[i][0]
 
        # initialize last column of right
        right[i][N - 1] = mat[i][N - 1]
 
    # fill all cells of above four matrix
    for i in range(N):
        for j in range(1, N):
             
            # calculate left matrix (filled
            # left to right)
            if (mat[i][j] == 1):
                left[i][j] = left[i][j - 1] + 1
            else:
                left[i][j] = 0
 
            # calculate top matrix
            if (mat[j][i] == 1):
                top[j][i] = top[j - 1][i] + 1
            else:
                top[j][i] = 0
 
            # calculate new value of j to calculate
            # value of bottom(i, j) and right(i, j)
            j = N - 1 - j
 
            # calculate bottom matrix
            if (mat[j][i] == 1):
                bottom[j][i] = bottom[j + 1][i] + 1
            else:
                bottom[j][i] = 0
 
            # calculate right matrix
            if (mat[i][j] == 1):
                right[i][j] = right[i][j + 1] + 1
            else:
                right[i][j] = 0
 
            # revert back to old j
            j = N - 1 - j
 
    # n stores length of longest '+'
    # found so far
    n = 0
 
    # compute longest +
    for i in range(N):
        for j in range(N):
             
            # find minimum of left(i, j),
            # right(i, j), top(i, j), bottom(i, j)
            l = min(min(top[i][j], bottom[i][j]),
                    min(left[i][j], right[i][j]))
 
            # largest + would be formed by
            # a cell that has maximum value
            if(l > n):
                n = l
 
    # 4 directions of length n - 1 and 1
    # for middle cell
    if (n):
        return 4 * (n - 1) + 1
 
    # matrix contains all 0's
    return 0
 
# Driver Code
if __name__=="__main__":
     
    # Binary Matrix of size N
    mat = [ [ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 ],
            [ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 ],
            [ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 ],
            [ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 ],
            [ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 ],
            [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 ],
            [ 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 ],
            [ 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 ],
            [ 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 ],
            [ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 ]]
 
    print(findLargestPlus(mat))
 
# This code is contributed by ChitraNayal


C#
// C# program to find the size of the largest '+'
// formed by all 1's in given binary matrix
using System;
 
class GFG {
     
    // size of binary square matrix
    static int N = 10;
 
    // Function to find the size of the largest '+'
    // formed by all 1's in given binary matrix
    static int findLargestPlus(int [,] mat)
    {
         
        // left[j][j], right[i][j], top[i][j] and
        // bottom[i][j] store maximum number of
        // consecutive 1's present to the left,
        // right, top and bottom of mat[i][j]
        // including cell(i, j) respectively
        int [,] left = new int[N,N];
        int [,] right = new int[N,N];
        int [,] top = new int[N,N];
        int [,] bottom = new int[N,N];
 
        // initialize above four matrix
        for (int i = 0; i < N; i++) {
             
            // initialize first row of top
            top[0,i] = mat[0,i];
 
            // initialize last row of bottom
            bottom[N - 1,i] = mat[N - 1,i];
 
            // initialize first column of left
            left[i,0] = mat[i,0];
 
            // initialize last column of right
            right[i,N - 1] = mat[i,N - 1];
        }
 
        // fill all cells of above four matrix
        for (int i = 0; i < N; i++) {
            for (int j = 1; j < N; j++) {
                 
                // calculate left matrix
                // (filled left to right)
                if (mat[i,j] == 1)
                    left[i,j] = left[i,j - 1] + 1;
                else
                    left[i,j] = 0;
 
                // calculate top matrix
                if (mat[j,i] == 1)
                    top[j,i] = top[j - 1,i] + 1;
                else
                    top[j,i] = 0;
 
                // calculate new value of j to
                // calculate value of bottom(i, j)
                // and right(i, j)
                j = N - 1 - j;
 
                // calculate bottom matrix
                if (mat[j,i] == 1)
                    bottom[j,i] = bottom[j + 1,i] + 1;
                else
                    bottom[j,i] = 0;
 
                // calculate right matrix
                if (mat[i,j] == 1)
                    right[i,j] = right[i,j + 1] + 1;
                else
                    right[i,j] = 0;
 
                // revert back to old j
                j = N - 1 - j;
            }
        }
 
        // n stores length of longest + found so far
        int n = 0;
 
        // compute longest +
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
 
                // find minimum of left(i, j),
                // right(i, j), top(i, j),
                // bottom(i, j)
                int len = Math.Min(Math.Min(top[i,j],
                    bottom[i,j]),Math.Min(left[i,j],
                                        right[i,j]));
 
                // largest + would be formed by a
                // cell that has maximum value
                if (len > n)
                    n = len;
            }
        }
 
        // 4 directions of length n - 1 and 1 for
        // middle cell
        if (n > 0)
            return 4 * (n - 1) + 1;
 
        // matrix contains all 0's
        return 0;
    }
 
    /* Driver function to test above functions */
    public static void Main()
    {
         
        // Binary Matrix of size N
        int [,]mat = {
            { 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
            { 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
            { 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
            { 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 },
            { 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
            { 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
            { 1, 0, 1, 1, 1, 1, 0, 0, 1, 1 },
            { 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 },
            { 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 }
        };
 
        Console.Write(findLargestPlus(mat));
    }
}
 
// This code is contributed by KRV.


PHP
 $n)
                $n = $len;
        }
    }
 
    // 4 directions of length n - 1 and 1
    // for middle cell
    if ($n)
    return 4 * ($n - 1) + 1;
 
    // matrix contains all 0's
    return 0;
}
 
// Driver Code
 
// Binary Matrix of size N
$mat = array(array(1, 0, 1, 1, 1, 1, 0, 1, 1, 1),
             array(1, 0, 1, 0, 1, 1, 1, 0, 1, 1),
             array(1, 1, 1, 0, 1, 1, 0, 1, 0, 1),
             array(0, 0, 0, 0, 1, 0, 0, 1, 0, 0),
             array(1, 1, 1, 0, 1, 1, 1, 1, 1, 1),
             array(1, 1, 1, 1, 1, 1, 1, 1, 1, 0),
             array(1, 0, 0, 0, 1, 0, 0, 1, 0, 1),
             array(1, 0, 1, 1, 1, 1, 0, 0, 1, 1),
             array(1, 1, 0, 0, 1, 0, 1, 0, 0, 1),
             array(1, 0, 1, 1, 1, 1, 0, 1, 0, 0));
 
echo findLargestPlus($mat);
 
// This code is contributed by Sach_Code
?>


Javascript


输出:

17