Una pregunta de array booleana

Dada una array booleana mat[M][N] de tamaño MXN, modifíquela de tal manera que si una celda matricial mat[i][j] es 1 (o verdadera), haga que todas las celdas de la i-ésima fila y la j-ésima columna sean 1. 
 

Example 1
The matrix
1 0
0 0
should be changed to following
1 1
1 0

Example 2
The matrix
0 0 0
0 0 1
should be changed to following
0 0 1
1 1 1

Example 3
The matrix
1 0 0 1
0 0 1 0
0 0 0 0
should be changed to following
1 1 1 1
1 1 1 1
1 0 1 1

Método 1 (Utilice dos arrays temporales) 
1) Cree dos arrays temporales fila[M] y col[N]. Inicialice todos los valores de fila[] y col[] como 0. 
2) Recorra la array de entrada mat[M][N]. Si ve una entrada mat[i][j] como verdadera, marque fila[i] y col[j] como verdaderas. 
3) Recorra la array de entrada mat[M][N] nuevamente. Para cada entrada mat[i][j], verifique los valores de fila[i] y col[j]. Si cualquiera de los dos valores (row[i] o col[j]) es verdadero, entonces marque mat[i][j] como verdadero.
Gracias a Dixit Sethi por sugerir este método. 
 

C++

// C++ Code For A Boolean Matrix Question
#include <bits/stdc++.h>
 
using namespace std;
#define R 3
#define C 4
 
void modifyMatrix(bool mat[R][C])
{
    bool row[R];
    bool col[C];
 
    int i, j;
 
    /* Initialize all values of row[] as 0 */
    for (i = 0; i < R; i++)
        row[i] = 0;
 
    /* Initialize all values of col[] as 0 */
    for (i = 0; i < C; i++)
        col[i] = 0;
 
    // Store the rows and columns to be marked as
    // 1 in row[] and col[] arrays respectively
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++) {
            if (mat[i][j] == 1) {
                row[i] = 1;
                col[j] = 1;
            }
        }
    }
 
    // Modify the input matrix mat[] using the
    // above constructed row[] and col[] arrays
    for (i = 0; i < R; i++)
        for (j = 0; j < C; j++)
            if (row[i] == 1 || col[j] == 1)
                mat[i][j] = 1;
}
 
/* A utility function to print a 2D matrix */
void printMatrix(bool mat[R][C])
{
    int i, j;
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++)
            cout << mat[i][j];
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    bool mat[R][C] = { { 1, 0, 0, 1 },
                       { 0, 0, 1, 0 },
                       { 0, 0, 0, 0 } };
 
    cout << "Input Matrix \n";
    printMatrix(mat);
    modifyMatrix(mat);
    printf("Matrix after modification \n");
    printMatrix(mat);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// C Code For A Boolean Matrix Question
#include <stdbool.h>
#include <stdio.h>
 
#define R 3
#define C 4
 
void modifyMatrix(bool mat[R][C])
{
    bool row[R];
    bool col[C];
 
    int i, j;
 
    /* Initialize all values of row[] as 0 */
    for (i = 0; i < R; i++)
        row[i] = 0;
 
    /* Initialize all values of col[] as 0 */
    for (i = 0; i < C; i++)
        col[i] = 0;
 
    // Store the rows and columns to be marked as
    // 1 in row[] and col[] arrays respectively
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++) {
            if (mat[i][j] == 1) {
                row[i] = 1;
                col[j] = 1;
            }
        }
    }
 
    // Modify the input matrix mat[] using the
    // above constructed row[] and col[] arrays
    for (i = 0; i < R; i++)
        for (j = 0; j < C; j++)
            if (row[i] == 1 || col[j] == 1)
                mat[i][j] = 1;
}
 
/* A utility function to print a 2D matrix */
void printMatrix(bool mat[R][C])
{
    int i, j;
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++)
            printf("%d ", mat[i][j]);
        printf("\n");
    }
}
 
// Driver Code
int main()
{
    bool mat[R][C] = { { 1, 0, 0, 1 },
                       { 0, 0, 1, 0 },
                       { 0, 0, 0, 0 } };
 
    printf("Input Matrix \n");
    printMatrix(mat);
    modifyMatrix(mat);
    printf("Matrix after modification \n");
    printMatrix(mat);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

// Java Code For A Boolean Matrix Question
class GFG {
    public static void modifyMatrix(int mat[][], int R,
                                    int C)
    {
        int row[] = new int[R];
        int col[] = new int[C];
        int i, j;
 
        /* Initialize all values of row[] as 0 */
        for (i = 0; i < R; i++)
            row[i] = 0;
 
        /* Initialize all values of col[] as 0 */
        for (i = 0; i < C; i++)
            col[i] = 0;
 
        /* Store the rows and columns to be marked as
        1 in row[] and col[] arrays respectively */
        for (i = 0; i < R; i++) {
            for (j = 0; j < C; j++) {
                if (mat[i][j] == 1) {
                    row[i] = 1;
                    col[j] = 1;
                }
            }
        }
 
        /* Modify the input matrix mat[] using the
        above constructed row[] and col[] arrays */
        for (i = 0; i < R; i++)
            for (j = 0; j < C; j++)
                if (row[i] == 1 || col[j] == 1)
                    mat[i][j] = 1;
    }
 
    /* A utility function to print a 2D matrix */
    public static void printMatrix(int mat[][], int R,
                                   int C)
    {
        int i, j;
        for (i = 0; i < R; i++) {
            for (j = 0; j < C; j++)
                System.out.print(mat[i][j] + " ");
            System.out.println();
        }
    }
 
    /* Driver program to test above functions */
    public static void main(String[] args)
    {
        int mat[][] = {
            { 1, 0, 0, 1 },
            { 0, 0, 1, 0 },
            { 0, 0, 0, 0 },
        };
 
        System.out.println("Matrix Initially");
        printMatrix(mat, 3, 4);
        modifyMatrix(mat, 3, 4);
        System.out.println("Matrix after modification n");
        printMatrix(mat, 3, 4);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3

# Python3 Code For A Boolean Matrix Question
R = 3
C = 4
 
def modifyMatrix(mat):
    row = [0] * R
    col = [0] * C
     
    # Initialize all values of row[] as 0
    for i in range(0, R):
        row[i] = 0
         
    # Initialize all values of col[] as 0
    for i in range(0, C) :
        col[i] = 0
 
 
    # Store the rows and columns to be marked
    # as 1 in row[] and col[] arrays respectively
    for i in range(0, R) :
         
        for j in range(0, C) :
            if (mat[i][j] == 1) :
                row[i] = 1
                col[j] = 1
             
    # Modify the input matrix mat[] using the
    # above constructed row[] and col[] arrays
    for i in range(0, R) :
         
        for j in range(0, C):
            if ( row[i] == 1 or col[j] == 1 ) :
                mat[i][j] = 1
                 
# A utility function to print a 2D matrix
def printMatrix(mat) :
    for i in range(0, R):
         
        for j in range(0, C) :
            print(mat[i][j], end = " ")
        print()
         
# Driver Code
mat = [ [1, 0, 0, 1],
        [0, 0, 1, 0],
        [0, 0, 0, 0] ]
 
print("Input Matrix n")
printMatrix(mat)
 
modifyMatrix(mat)
 
print("Matrix after modification n")
printMatrix(mat)
 
# This code is contributed by Nikita Tiwari.

C#

// C# Code For A Boolean
// Matrix Question
using System;
 
class GFG
{
    public static void modifyMatrix(int [,]mat,
                                    int R, int C)
    {
        int []row = new int [R];
        int []col = new int [C];
        int i, j;
     
        /* Initialize all values
        of row[] as 0 */
        for (i = 0; i < R; i++)
        {
        row[i] = 0;
        }
     
     
        /* Initialize all values
        of col[] as 0 */
        for (i = 0; i < C; i++)
        {
        col[i] = 0;
        }
     
     
        /* Store the rows and columns
        to be marked as 1 in row[]
        and col[] arrays respectively */
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                if (mat[i, j] == 1)
                {
                    row[i] = 1;
                    col[j] = 1;
                }
            }
        }
     
        /* Modify the input matrix
        mat[] using the above
        constructed row[] and
        col[] arrays */
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                if (row[i] == 1 || col[j] == 1)
                {
                    mat[i, j] = 1;
                }
            }
        }
    }
     
    /* A utility function to
    print a 2D matrix */
    public static void printMatrix(int [,]mat,
                                   int R, int C)
    {
        int i, j;
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                Console.Write(mat[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
     
    // Driver code
    static public void Main ()
    {
        int [,]mat = {{1, 0, 0, 1},
                      {0, 0, 1, 0},
                      {0, 0, 0, 0}};
             
        Console.WriteLine("Matrix Initially");
         
        printMatrix(mat, 3, 4);
     
        modifyMatrix(mat, 3, 4);
        Console.WriteLine("Matrix after "+
                        "modification n");
        printMatrix(mat, 3, 4);
         
    }
}
 
// This code is contributed by ajit

PHP

<?php
// PHP Code For A Boolean
// Matrix Question
$R = 3;
$C = 4;
 
function modifyMatrix(&$mat)
{
    global $R,$C;
    $row = array();
    $col = array();
 
    /* Initialize all values
       of row[] as 0 */
    for ($i = 0; $i < $R; $i++)
    {
        $row[$i] = 0;
    }
 
 
    /* Initialize all values
       of col[] as 0 */
    for ($i = 0; $i < $C; $i++)
    {
        $col[$i] = 0;
    }
 
 
    /* Store the rows and columns
       to be marked as 1 in row[]
       and col[] arrays respectively */
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
        {
            if ($mat[$i][$j] == 1)
            {
                $row[$i] = 1;
                $col[$j] = 1;
            }
        }
    }
 
    /* Modify the input matrix mat[]
       using the above constructed
       row[] and col[] arrays */
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
        {
            if ($row[$i] == 1 ||
                $col[$j] == 1 )
            {
                $mat[$i][$j] = 1;
            }
        }
    }
}
 
/* A utility function to
   print a 2D matrix */
function printMatrix(&$mat)
{
    global $R, $C;
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
        {
            echo $mat[$i][$j] . " ";
        }
        echo "\n";
    }
}
 
// Driver code
$mat = array(array(1, 0, 0, 1),
             array(0, 0, 1, 0),
             array(0, 0, 0, 0));
 
echo "Input Matrix \n";
printMatrix($mat);
 
modifyMatrix($mat);
 
echo "Matrix after modification \n";
printMatrix($mat);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
// Javascript Code For A Boolean Matrix Question
     
    function modifyMatrix(mat,R,C)
    {
        let row=new Array(R);
        let col = new Array(C);
         
        /* Initialize all values of row[] as 0 */
        for (i = 0; i < R; i++)
        {
            row[i] = 0;
        }
         
        /* Initialize all values of col[] as 0 */
        for (i = 0; i < C; i++)
        {
            col[i] = 0;
             
        }
         
        /* Store the rows and columns to be marked as
        1 in row[] and col[] arrays respectively */
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                if (mat[i][j] == 1)
                {
                    row[i] = 1;
                    col[j] = 1;
                }
            }
        }
         
        /* Modify the input matrix mat[] using the
        above constructed row[] and col[] arrays */
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                if ( row[i] == 1 || col[j] == 1 )
                {
                    mat[i][j] = 1;
                }
            }
        }
    }
     
    /* A utility function to print a 2D matrix */
    function printMatrix(mat,R,C)
    {
        let i, j;
        for (i = 0; i < R; i++)
        {
            for (j = 0; j < C; j++)
            {
                document.write(mat[i][j]+ " ");
            }
            document.write("<br>");
        }
    }
     
    /* Driver program to test above functions */
     
    let mat = [[1, 0, 0, 1],[0, 0, 1, 0],[0, 0, 0, 0]];
    document.write("Matrix Initially <br>")
    printMatrix(mat, 3, 4);
    modifyMatrix(mat, 3, 4);
    document.write("Matrix after modification n <br>");
    printMatrix(mat, 3, 4);
     
    // This code is contributed by avanitrachhadiya2155
     
</script>

Producción:  

Input Matrix
1 0 0 1
0 0 1 0
0 0 0 0
Matrix after modification
1 1 1 1
1 1 1 1
1 0 1 1

Complejidad de tiempo: O(M*N) 
Espacio auxiliar: O(M + N)
Método 2 (una versión optimizada para el espacio del método 1) 
Este método es una versión optimizada para el espacio del método 1 anterior. Este método usa la primera fila y la primera columna de la array de entrada en lugar de las arrays auxiliares fila[] y col[] del método 1. Entonces, lo que hacemos es: primero cuidar la primera fila y la primera columna y almacenar la información sobre estos dos en dos variables indicadoras filaFlag y colFlag. Una vez que tengamos esta información, podemos usar la primera fila y la primera columna como arrays auxiliares y aplicar el método 1 para la subarray (array que excluye la primera fila y la primera columna) de tamaño (M-1)*(N-1).
1) Escanee la primera fila y configure una variable rowFlag para indicar si necesitamos configurar todos los 1 en la primera fila o no. 
2) Escanee la primera columna y configure una variable colFlag para indicar si necesitamos configurar todos los 1 en la primera columna o no. 
3) Use la primera fila y la primera columna como arreglos auxiliares fila[] y col[] respectivamente, considere la array como subarray comenzando desde la segunda fila y la segunda columna y aplique el método 1. 
4) Finalmente, usando filaFlag y colFlag, actualice la primera fila y la primera columna si es necesario.
Complejidad de tiempo: O(M*N) 
Espacio auxiliar: O(1)
Gracias a Sidh por sugerir este método. 
 

C++

#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 4
 
void modifyMatrix(int mat[R][C])
{
    // variables to check if there are any 1
    // in first row and column
    bool row_flag = false;
    bool col_flag = false;
 
    // updating the first row and col if 1
    // is encountered
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            if (i == 0 && mat[i][j] == 1)
                row_flag = true;
 
            if (j == 0 && mat[i][j] == 1)
                col_flag = true;
 
            if (mat[i][j] == 1) {
 
                mat[0][j] = 1;
                mat[i][0] = 1;
            }
        }
    }
 
    // Modify the input matrix mat[] using the
    // first row and first column of Matrix mat
    for (int i = 1; i < R; i++)
        for (int j = 1; j < C; j++)
            if (mat[0][j] == 1 || mat[i][0] == 1)
                mat[i][j] = 1;
 
    // modify first row if there was any 1
    if (row_flag == true)
        for (int i = 0; i < C; i++)
            mat[0][i] = 1;
 
    // modify first col if there was any 1
    if (col_flag == true)
        for (int i = 0; i < R; i++)
            mat[i][0] = 1;
}
 
/* A utility function to print a 2D matrix */
void printMatrix(int mat[R][C])
{
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++)
            cout << mat[i][j]<<" ";
        cout << "\n";
    }
}
 
// Driver function to test the above function
int main()
{
 
    int mat[R][C] = { { 1, 0, 0, 1 },
                      { 0, 0, 1, 0 },
                      { 0, 0, 0, 0 } };
 
    cout << "Input Matrix :\n";
    printMatrix(mat);
    modifyMatrix(mat);
    cout << "Matrix After Modification :\n";
    printMatrix(mat);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

#include <stdio.h>
#include <stdbool.h>
 
#define R 3
#define C 4
 
void modifyMatrix(int mat[R][C])
{
    // variables to check if there are any 1
    // in first row and column
    bool row_flag = false;
    bool col_flag = false;
 
    // updating the first row and col if 1
    // is encountered
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            if (i == 0 && mat[i][j] == 1)
                row_flag = true;
 
            if (j == 0 && mat[i][j] == 1)
                col_flag = true;
 
            if (mat[i][j] == 1) {
                mat[0][j] = 1;
                mat[i][0] = 1;
            }
        }
    }
 
    // Modify the input matrix mat[] using the
    // first row and first column of Matrix mat
    for (int i = 1; i < R; i++)
        for (int j = 1; j < C; j++)
            if (mat[0][j] == 1 || mat[i][0] == 1)
                mat[i][j] = 1;
 
    // modify first row if there was any 1
    if (row_flag == true)
        for (int i = 0; i < C; i++)
            mat[0][i] = 1;
 
    // modify first col if there was any 1
    if (col_flag == true)
        for (int i = 0; i < R; i++)
            mat[i][0] = 1;
}
 
/* A utility function to print a 2D matrix */
void printMatrix(int mat[R][C])
{
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++)
            printf("%d ",mat[i][j]);
        printf("\n");
    }
}
 
// Driver function to test the above function
int main()
{
 
    int mat[R][C] = { { 1, 0, 0, 1 },
                      { 0, 0, 1, 0 },
                      { 0, 0, 0, 0 } };
 
    printf("Input Matrix :\n");
    printMatrix(mat);
    modifyMatrix(mat);
    printf("Matrix After Modification :\n");
    printMatrix(mat);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

class GFG {
    public static void modifyMatrix(int mat[][])
    {
        // variables to check if there are any 1
        // in first row and column
        boolean row_flag = false;
        boolean col_flag = false;
 
        // updating the first row and col if 1
        // is encountered
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                if (i == 0 && mat[i][j] == 1)
                    row_flag = true;
 
                if (j == 0 && mat[i][j] == 1)
                    col_flag = true;
 
                if (mat[i][j] == 1) {
 
                    mat[0][j] = 1;
                    mat[i][0] = 1;
                }
            }
        }
 
        // Modify the input matrix mat[] using the
        // first row and first column of Matrix mat
        for (int i = 1; i < mat.length; i++)
            for (int j = 1; j < mat[0].length; j++)
                if (mat[0][j] == 1 || mat[i][0] == 1)
                    mat[i][j] = 1;
 
        // modify first row if there was any 1
        if (row_flag == true)
            for (int i = 0; i < mat[0].length; i++)
                mat[0][i] = 1;
 
        // modify first col if there was any 1
        if (col_flag == true)
            for (int i = 0; i < mat.length; i++)
                mat[i][0] = 1;
    }
 
    /* A utility function to print a 2D matrix */
    public static void printMatrix(int mat[][])
    {
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++)
                System.out.print(mat[i][j]+ " ");
            System.out.println("");
        }
    }
 
    // Driver function to test the above function
    public static void main(String args[])
    {
        int mat[][] = { { 1, 0, 0, 1 },
                        { 0, 0, 1, 0 },
                        { 0, 0, 0, 0 } };
 
        System.out.println("Input Matrix :");
        printMatrix(mat);
        modifyMatrix(mat);
        System.out.println("Matrix After Modification :");
        printMatrix(mat);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3

# Python3 Code For A Boolean Matrix Question
def modifyMatrix(mat) :
     
    # variables to check if there are any 1
    # in first row and column
    row_flag = False
    col_flag = False
             
    # updating the first row and col
    # if 1 is encountered
    for i in range(0, len(mat)) :
         
        for j in range(0, len(mat)) :
            if (i == 0 and mat[i][j] == 1) :
                row_flag = True
                     
            if (j == 0 and mat[i][j] == 1) :
                col_flag = True
             
            if (mat[i][j] == 1) :
                mat[0][j] = 1
                mat[i][0] = 1
                 
    # Modify the input matrix mat[] using the
    # first row and first column of Matrix mat
    for i in range(1, len(mat)) :
         
        for j in range(1, len(mat) + 1) :
            if (mat[0][j] == 1 or mat[i][0] == 1) :
                mat[i][j] = 1
                 
    # modify first row if there was any 1
    if (row_flag == True) :
        for i in range(0, len(mat)) :
            mat[0][i] = 1
             
    # modify first col if there was any 1
    if (col_flag == True) :
        for i in range(0, len(mat)) :
            mat[i][0] = 1
             
# A utility function to print a 2D matrix
def printMatrix(mat) :
     
    for i in range(0, len(mat)) :
        for j in range(0, len(mat) + 1) :
            print( mat[i][j], end = "" )
         
        print()
         
# Driver Code
mat = [ [1, 0, 0, 1],
        [0, 0, 1, 0],
        [0, 0, 0, 0] ]
         
print("Input Matrix :")
printMatrix(mat)
     
modifyMatrix(mat)
     
print("Matrix After Modification :")
printMatrix(mat)
 
# This code is contributed by Nikita tiwari.

C#

// C# Code For A Boolean
// Matrix Question
using System;
 
class GFG
{
    public static void modifyMatrix(int[,] mat)
    {
                 
        // variables to check
        // if there are any 1
        // in first row and column
        bool row_flag = false;
        bool col_flag = false;
                 
        // updating the first
        // row and col if 1
        // is encountered
        for (int i = 0;
                 i < mat.GetLength(0); i++ )
        {
                for (int j = 0;
                         j < mat.GetLength(1); j++)
                {
                        if (i == 0 && mat[i, j] == 1)
                            row_flag = true;
                         
                        if (j == 0 && mat[i, j] == 1)
                            col_flag = true;
                         
                        if (mat[i, j] == 1)
                        {
                            mat[0, j] = 1;
                            mat[i,0] = 1;
                        }
                         
                    }
                }
                 
        // Modify the input matrix mat[]
        // using the first row and first
        // column of Matrix mat
        for (int i = 1;
                 i < mat.GetLength(0); i ++)
        {
                for (int j = 1;
                         j < mat.GetLength(1); j ++)
                {
                         
                    if (mat[0, j] == 1 ||
                        mat[i, 0] == 1)
                    {
                            mat[i, j] = 1;
                        }
                    }
                }
                 
        // modify first row
        // if there was any 1
        if (row_flag == true)
        {
            for (int i = 0;
                     i < mat.GetLength(1); i++)
            {
                        mat[0, i] = 1;
            }
        }
                 
        // modify first col if
        // there was any 1
        if (col_flag == true)
        {
            for (int i = 0;
                     i < mat.GetLength(0); i ++)
            {
                        mat[i, 0] = 1;
            }
        }
    }
             
    /* A utility function
    to print a 2D matrix */
    public static void printMatrix(int[,] mat)
    {
        for (int i = 0;
                 i < mat.GetLength(0); i ++)
        {
            for (int j = 0;
                     j < mat.GetLength(1); j ++)
            {
                Console.Write(mat[i, j] + " " );
            }
                Console.Write("\n");
        }
    }
             
    // Driver Code
    public static void Main()
    {
        int[,] mat = {{1, 0, 0, 1},
                      {0, 0, 1, 0},
                      {0, 0, 0, 0}};
                 
        Console.Write("Input Matrix :\n");
        printMatrix(mat);
             
        modifyMatrix(mat);
             
        Console.Write("Matrix After " +
                      "Modification :\n");
        printMatrix(mat);
    }
}
 
// This code is contributed
// by ChitraNayal

PHP

<?php
// PHP Code For A Boolean
// Matrix Question
$R = 3;
$C = 4;
 
function modifyMatrix(&$mat)
{
    global $R, $C;
     
    // variables to check if
    // there are any 1 in
    // first row and column
    $row_flag = false;
    $col_flag = false;
 
    // updating the first
    // row and col if 1
    // is encountered
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
        {
            if ($i == 0 && $mat[$i][$j] == 1)
                $row_flag = true;
 
            if ($j == 0 && $mat[$i][$j] == 1)
                $col_flag = true;
 
            if ($mat[$i][$j] == 1)
            {
                $mat[0][$j] = 1;
                $mat[$i][0] = 1;
            }
        }
    }
 
    // Modify the input matrix
    // mat[] using the first
    // row and first column of
    // Matrix mat
    for ($i = 1; $i < $R; $i++)
    {
        for ($j = 1; $j < $C; $j++)
        {
            if ($mat[0][$j] == 1 ||
                $mat[$i][0] == 1)
            {
                $mat[$i][$j] = 1;
            }
        }
    }
 
    // modify first row
    // if there was any 1
    if ($row_flag == true)
    {
        for ($i = 0; $i < $C; $i++)
        {
            $mat[0][$i] = 1;
        }
    }
 
    // modify first col
    // if there was any 1
    if ($col_flag == true)
    {
        for ($i = 0; $i < $R; $i++)
        {
            $mat[$i][0] = 1;
        }
    }
}
 
/* A utility function
to print a 2D matrix */
function printMatrix(&$mat)
{
    global $R, $C;
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
        {
            echo $mat[$i][$j]." ";
        }
        echo "\n";
    }
}
 
// Driver Code
$mat = array(array(1, 0, 0, 1 ),
             array(0, 0, 1, 0 ),
             array(0, 0, 0, 0 ));
 
echo "Input Matrix :\n";
printMatrix($mat);
 
modifyMatrix($mat);
 
echo "Matrix After Modification :\n";
printMatrix($mat);
 
// This code is contributed
// by ChitraNayal
?>

Javascript

<script>
 
function modifyMatrix(mat)
{
    // variables to check if there are any 1
        // in first row and column
        let row_flag = false;
        let col_flag = false;
                  
        // updating the first row and col if 1
        // is encountered
        for (let i = 0; i < mat.length; i++ ){
                for (let j = 0; j < mat[0].length; j++){
                        if (i == 0 && mat[i][j] == 1)
                            row_flag = true;
                          
                        if (j == 0 && mat[i][j] == 1)
                            col_flag = true;
                          
                        if (mat[i][j] == 1){
                              
                            mat[0][j] = 1;
                            mat[i][0] = 1;
                        }
                          
                    }
                }
                  
        // Modify the input matrix mat[] using the
        // first row and first column of Matrix mat
        for (let i = 1; i < mat.length; i ++){
                for (let j = 1; j < mat[0].length; j ++){
                          
                    if (mat[0][j] == 1 || mat[i][0] == 1){
                            mat[i][j] = 1;
                        }
                    }
                }
                  
        // modify first row if there was any 1
        if (row_flag == true){
            for (let i = 0; i < mat[0].length; i++){
                        mat[0][i] = 1;
                    }
                }
                  
        // modify first col if there was any 1
        if (col_flag == true){
            for (let i = 0; i < mat.length; i ++){
                        mat[i][0] = 1;
            }
        }
}
 
/* A utility function to print a 2D matrix */
function printMatrix(mat)
{
    for (let i = 0; i < mat.length; i ++){
            for (let j = 0; j < mat[0].length; j ++){
                document.write( mat[i][j] );
            }
                document.write("<br>");
        }
}
 
// Driver function to test the above function
let mat=[[1, 0, 0, 1],
                [0, 0, 1, 0],
                [0, 0, 0, 0]];
document.write("Input Matrix :<br>");
printMatrix(mat);
 
modifyMatrix(mat);
 
document.write("Matrix After Modification :<br>");
printMatrix(mat);
 
// This code is contributed by ab2127
 
</script>

Producción:  

Input Matrix :
1001
0010
0000
Matrix After Modification :
1111
1111
1011

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *