Le plus grand plus ou « + » formé par tous les uns dans une matrice carrée binaire
Compte tenu d'un n × n matrice binaire avec composé de 0s et 1s . Votre tâche est de trouver la taille du plus grand '+' forme qui peut être formée en utilisant uniquement 1s .
UN '+' La forme se compose d'une cellule centrale avec quatre bras s'étendant dans les quatre directions ( en haut en bas à gauche et à droite ) tout en restant dans les limites de la matrice. La taille d'un '+' est défini comme le nombre total de cellules le formant comprenant le centre et tous les bras.
La tâche est de restituer le taille maximale de tout valide '+' dans avec . Si non '+' peut être formé retour .
Exemples :
Saisir: avec = [ [0 1 1 0 1] [0 0 1 1 1] [1 1 1 1 1] [1 1 1 0 1] [0 1 1 1 0] ]
Sortir: 9
Explication: Un « + » avec une longueur de bras de 2 (2 cellules dans chaque direction + 1 centre) peut être formé au centre du tapis.
0 1 1 0 1
0 0 1 1 1
1 1 1 1 1
1 1 1 0 1
0 1 1 1 0
Taille totale = (2 × 4) + 1 = 9Saisir: avec = [ [0 1 1] [0 0 1] [1 1 1] ]
Sortir: 1
Explication: Un «+» avec une longueur de bras de 0 (0 cellule dans chaque direction + 1 centre) peut être formé avec n'importe lequel des 1.Saisir: avec = [ [0] ]
Sortir:
Explication: Non Le signe « + » peut être formé.
[Approche naïve] - Considérez chaque point comme centre - O(n^4) Temps et O(n^4) Espace
Parcourez les cellules de la matrice une par une. Considérez chaque point parcouru comme le centre d'un plus et trouvez la taille du +. Pour chaque élément, nous parcourons la gauche, la droite, le bas et le haut. Le pire des cas dans cette solution se produit lorsque nous avons tous des 1.
[Approche attendue] - Précalculer 4 tableaux - O(n^2) Temps et O(n^2) Espace
Le idée est de maintenir quatre matrices auxiliaires gauche[][] droite[][] haut[][] bas[][] pour stocker des 1 consécutifs dans toutes les directions. Pour chaque cellule (je j) dans la matrice d'entrée, nous stockons les informations ci-dessous dans ces quatre matrices -
- à gauche (je j) stocke le nombre maximum de 1 consécutifs dans le gauche de la cellule (i j) incluant la cellule (i j).
- c'est vrai (je j) stocke le nombre maximum de 1 consécutifs dans le droite de la cellule (i j) incluant la cellule (i j).
- haut (je j) stocke le nombre maximum de 1 consécutifs à haut de la cellule (i j) incluant la cellule (i j).
- en bas (je j) stocke le nombre maximum de 1 consécutifs à bas de la cellule (i j) incluant la cellule (i j).
Après avoir calculé la valeur de chaque cellule des matrices ci-dessus, le le plus grand'+' serait formé par une cellule de matrice d'entrée qui a une valeur maximale en considérant le minimum de ( gauche (i j) droite (i j) haut (i j) bas (i j) )
Nous pouvons utiliser Programmation dynamique pour calculer le nombre total de 1 consécutifs dans toutes les directions :
si mat(i j) == 1
gauche (i j) = gauche (i j - 1) + 1sinon gauche (i j) = 0
si mat(i j) == 1
haut(je j) = haut(je - 1 j) + 1;sinon top(i j) = 0;
si mat(i j) == 1
bas (je j) = bas (je + 1 j) + 1;sinon bottom(i j) = 0;
si mat(i j) == 1
droite(je j) = droite(je j + 1) + 1;sinon c'est vrai(i j) = 0;
Vous trouverez ci-dessous la mise en œuvre de l’approche ci-dessus :
C++ // C++ program to find the largest '+' in a binary matrix // using Dynamic Programming #include using namespace std ; int findLargestPlus ( vector < vector < int >> & mat ) { int n = mat . size (); vector < vector < int >> left ( n vector < int > ( n 0 )); vector < vector < int >> right ( n vector < int > ( n 0 )); vector < vector < int >> top ( n vector < int > ( n 0 )); vector < vector < int >> bottom ( n vector < int > ( n 0 )); // Fill left and top matrices for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] == 1 ) { left [ i ][ j ] = ( j == 0 ) ? 1 : left [ i ][ j - 1 ] + 1 ; top [ i ][ j ] = ( i == 0 ) ? 1 : top [ i - 1 ][ j ] + 1 ; } } } // Fill right and bottom matrices for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( mat [ i ][ j ] == 1 ) { right [ i ][ j ] = ( j == n - 1 ) ? 1 : right [ i ][ j + 1 ] + 1 ; bottom [ i ][ j ] = ( i == n - 1 ) ? 1 : bottom [ i + 1 ][ j ] + 1 ; } } } int maxPlusSize = 0 ; // Compute the maximum '+' size for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] == 1 ) { int armLength = min ({ left [ i ][ j ] right [ i ][ j ] top [ i ][ j ] bottom [ i ][ j ]}); maxPlusSize = max ( maxPlusSize ( 4 * ( armLength - 1 )) + 1 ); } } } return maxPlusSize ; } int main () { // Hardcoded input matrix vector < vector < int >> mat = { { 0 1 1 0 1 } { 0 0 1 1 1 } { 1 1 1 1 1 } { 1 1 1 0 1 } { 0 1 1 1 0 } }; cout < < findLargestPlus ( mat ) < < endl ; return 0 ; }
Java // Java program to find the largest '+' in a binary matrix // using Dynamic Programming class GfG { static int findLargestPlus ( int [][] mat ) { int n = mat . length ; int [][] left = new int [ n ][ n ] ; int [][] right = new int [ n ][ n ] ; int [][] top = new int [ n ][ n ] ; int [][] bottom = new int [ n ][ n ] ; // Fill left and top matrices for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] == 1 ) { left [ i ][ j ] = ( j == 0 ) ? 1 : left [ i ][ j - 1 ] + 1 ; top [ i ][ j ] = ( i == 0 ) ? 1 : top [ i - 1 ][ j ] + 1 ; } } } // Fill right and bottom matrices for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( mat [ i ][ j ] == 1 ) { right [ i ][ j ] = ( j == n - 1 ) ? 1 : right [ i ][ j + 1 ] + 1 ; bottom [ i ][ j ] = ( i == n - 1 ) ? 1 : bottom [ i + 1 ][ j ] + 1 ; } } } int maxPlusSize = 0 ; // Compute the maximum '+' size for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] == 1 ) { int armLength = Math . min ( Math . min ( left [ i ][ j ] right [ i ][ j ] ) Math . min ( top [ i ][ j ] bottom [ i ][ j ] )); maxPlusSize = Math . max ( maxPlusSize ( 4 * ( armLength - 1 )) + 1 ); } } } return maxPlusSize ; } public static void main ( String [] args ) { // Hardcoded input matrix int [][] mat = { { 0 1 1 0 1 } { 0 0 1 1 1 } { 1 1 1 1 1 } { 1 1 1 0 1 } { 0 1 1 1 0 } }; System . out . println ( findLargestPlus ( mat )); } }
Python # Python program to find the largest '+' in a binary matrix # using Dynamic Programming def findLargestPlus ( mat ): n = len ( mat ) left = [[ 0 ] * n for i in range ( n )] right = [[ 0 ] * n for i in range ( n )] top = [[ 0 ] * n for i in range ( n )] bottom = [[ 0 ] * n for i in range ( n )] # Fill left and top matrices for i in range ( n ): for j in range ( n ): if mat [ i ][ j ] == 1 : left [ i ][ j ] = 1 if j == 0 else left [ i ][ j - 1 ] + 1 top [ i ][ j ] = 1 if i == 0 else top [ i - 1 ][ j ] + 1 # Fill right and bottom matrices for i in range ( n - 1 - 1 - 1 ): for j in range ( n - 1 - 1 - 1 ): if mat [ i ][ j ] == 1 : right [ i ][ j ] = 1 if j == n - 1 else right [ i ][ j + 1 ] + 1 bottom [ i ][ j ] = 1 if i == n - 1 else bottom [ i + 1 ][ j ] + 1 maxPlusSize = 0 # Compute the maximum '+' size for i in range ( n ): for j in range ( n ): if mat [ i ][ j ] == 1 : armLength = min ( left [ i ][ j ] right [ i ][ j ] top [ i ][ j ] bottom [ i ][ j ]) maxPlusSize = max ( maxPlusSize ( 4 * ( armLength - 1 )) + 1 ) return maxPlusSize if __name__ == '__main__' : # Hardcoded input matrix mat = [ [ 0 1 1 0 1 ] [ 0 0 1 1 1 ] [ 1 1 1 1 1 ] [ 1 1 1 0 1 ] [ 0 1 1 1 0 ] ] print ( findLargestPlus ( mat ))
C# // C# program to find the largest '+' in a binary matrix // using Dynamic Programming using System ; class GfG { static int FindLargestPlus ( int [] mat ) { int n = mat . GetLength ( 0 ); int [] left = new int [ n n ]; int [] right = new int [ n n ]; int [] top = new int [ n n ]; int [] bottom = new int [ n n ]; // Fill left and top matrices for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i j ] == 1 ) { left [ i j ] = ( j == 0 ) ? 1 : left [ i j - 1 ] + 1 ; top [ i j ] = ( i == 0 ) ? 1 : top [ i - 1 j ] + 1 ; } } } // Fill right and bottom matrices for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( mat [ i j ] == 1 ) { right [ i j ] = ( j == n - 1 ) ? 1 : right [ i j + 1 ] + 1 ; bottom [ i j ] = ( i == n - 1 ) ? 1 : bottom [ i + 1 j ] + 1 ; } } } int maxPlusSize = 0 ; // Compute the maximum '+' size for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i j ] == 1 ) { int armLength = Math . Min ( Math . Min ( left [ i j ] right [ i j ]) Math . Min ( top [ i j ] bottom [ i j ])); maxPlusSize = Math . Max ( maxPlusSize ( 4 * ( armLength - 1 )) + 1 ); } } } return maxPlusSize ; } public static void Main () { // Hardcoded input matrix int [] mat = { { 0 1 1 0 1 } { 0 0 1 1 1 } { 1 1 1 1 1 } { 1 1 1 0 1 } { 0 1 1 1 0 } }; Console . WriteLine ( FindLargestPlus ( mat )); } }
JavaScript // JavaScript program to find the largest '+' in a binary matrix // using Dynamic Programming function findLargestPlus ( mat ) { let n = mat . length ; let left = Array . from ({ length : n } () => Array ( n ). fill ( 0 )); let right = Array . from ({ length : n } () => Array ( n ). fill ( 0 )); let top = Array . from ({ length : n } () => Array ( n ). fill ( 0 )); let bottom = Array . from ({ length : n } () => Array ( n ). fill ( 0 )); // Fill left and top matrices for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] === 1 ) { left [ i ][ j ] = ( j === 0 ) ? 1 : left [ i ][ j - 1 ] + 1 ; top [ i ][ j ] = ( i === 0 ) ? 1 : top [ i - 1 ][ j ] + 1 ; } } } // Fill right and bottom matrices for ( let i = n - 1 ; i >= 0 ; i -- ) { for ( let j = n - 1 ; j >= 0 ; j -- ) { if ( mat [ i ][ j ] === 1 ) { right [ i ][ j ] = ( j === n - 1 ) ? 1 : right [ i ][ j + 1 ] + 1 ; bottom [ i ][ j ] = ( i === n - 1 ) ? 1 : bottom [ i + 1 ][ j ] + 1 ; } } } let maxPlusSize = 0 ; // Compute the maximum '+' size for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( mat [ i ][ j ] === 1 ) { let armLength = Math . min ( left [ i ][ j ] right [ i ][ j ] top [ i ][ j ] bottom [ i ][ j ]); maxPlusSize = Math . max ( maxPlusSize ( 4 * ( armLength - 1 )) + 1 ); } } } return maxPlusSize ; } // Hardcoded input matrix let mat = [ [ 0 1 1 0 1 ] [ 0 0 1 1 1 ] [ 1 1 1 1 1 ] [ 1 1 1 0 1 ] [ 0 1 1 1 0 ] ]; console . log ( findLargestPlus ( mat ));
Sortir
9
Complexité temporelle : O(n²) grâce à quatre passes pour calculer les matrices directionnelles et une passe finale pour déterminer le plus grand '+'. Chaque passe prend un temps O(n²), ce qui conduit à une complexité globale de O(n²).
Complexité spatiale : O(n²) en raison de quatre matrices auxiliaires (gauche droite en haut en bas) consommant O (n²) d'espace supplémentaire.