Compruebe si Matrix permanece sin cambios después de las inversiones de fila

Dada una array NxN. La tarea es verificar si después de invertir todas las filas de la Array dada, la array sigue siendo la misma o no.

Ejemplos:  

Input : N = 3
1 2 1
2 2 2
3 4 3 
Output : Yes 
If all the rows are reversed then matrix will become:
1 2 1
2 2 2
3 4 3 
which is same.

Input : N = 3
1 2 2
2 2 2
3 4 3
Output : No 

Enfoque :  

  1. Una observación muy importante es que para que la array sea la misma después de las inversiones de las filas, cada fila individual debe ser palindrómica.
  2. Ahora, para verificar si una fila es palindrómica, mantenga dos punteros, uno apuntando al inicio y el otro al final de la fila. Comience a comparar los valores presentes y haga start++ y end–. Repita el proceso hasta que todos los elementos estén marcados hasta la mitad de la fila. Si en cada paso los elementos son los mismos, entonces la fila es palindrómica, de lo contrario no lo es.
  3. Si alguno de los Row no es palindrómico, la respuesta es No.

A continuación se muestra la implementación del enfoque anterior:  

C++

// C++ implementation of the above approach
 
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
 
// Function to check Palindromic Condition
void specialMatrix(int matrix[3][3], int N)
{
    for (int i = 0; i < N; i++) {
 
        // Pointer to start of row
        int start = 0;
 
        // Pointer to end of row
        int end = N - 1;
 
        while (start <= end) {
 
            // Single Mismatch means row is not palindromic
            if (matrix[i][start] != matrix[i][end]) {
                cout << "No" << endl;
                return;
            }
 
            start++;
            end--;
        }
    }
 
    cout << "Yes" << endl;
    return;
}
 
// Driver Code
int main()
{
    int matrix[3][3] = { { 1, 2, 1 },
                         { 2, 2, 2 },
                         { 3, 4, 3 } };
    int N = 3;
    specialMatrix(matrix, N);
 
    return 0;
}

Java

// Java implementation of the above approach
class GFG
{
 
// Function to check Palindromic Condition
static void specialMatrix(int matrix[][], int N)
{
    for (int i = 0; i < N; i++)
    {
 
        // Pointer to start of row
        int start = 0;
 
        // Pointer to end of row
        int end = N - 1;
 
        while (start <= end)
        {
 
            // Single Mismatch means
            // row is not palindromic
            if (matrix[i][start] != matrix[i][end])
            {
                System.out.println("No");
                return;
            }
 
            start++;
            end--;
        }
    }
 
    System.out.println("Yes");
    return;
}
 
// Driver Code
public static void main(String[] args)
{
    int matrix[][] = { { 1, 2, 1 },
                        { 2, 2, 2 },
                        { 3, 4, 3 } };
    int N = 3;
    specialMatrix(matrix, N);
}
}
 
/* This code contributed by PrinciRaj1992 */

Python3

# Python 3 implementation of the above approach
 
# Function to check Palindromic Condition
def specialMatrix(matrix, N):
    for i in range(N):
         
        # Pointer to start of row
        start = 0
 
        # Pointer to end of row
        end = N - 1
 
        while (start <= end):
             
            # Single Mismatch means row is not palindromic
            if (matrix[i][start] != matrix[i][end]):
                print("No")
                return
 
            start += 1
            end -= 1
 
    print("Yes")
    return
 
# Driver Code
if __name__ == '__main__':
    matrix = [[1, 2, 1], [2, 2, 2], [3, 4, 3]]
    N = 3
    specialMatrix(matrix, N)
     
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the above approach
using System;
 
class GFG
{
 
// Function to check Palindromic Condition
static void specialMatrix(int [,]matrix, int N)
{
    for (int i = 0; i < N; i++)
    {
 
        // Pointer to start of row
        int start = 0;
 
        // Pointer to end of row
        int end = N - 1;
 
        while (start <= end)
        {
 
            // Single Mismatch means
            // row is not palindromic
            if (matrix[i, start] != matrix[i, end])
            {
                Console.WriteLine("No");
                return;
            }
 
            start++;
            end--;
        }
    }
    Console.WriteLine("Yes");
    return;
}
 
// Driver Code
public static void Main(String[] args)
{
    int [,]matrix = { { 1, 2, 1 },
                        { 2, 2, 2 },
                        { 3, 4, 3 } };
    int N = 3;
    specialMatrix(matrix, N);
}
}
 
// This code has been contributed by 29AjayKumar

PHP

<?php
// PHP implementation of the above approach
 
// Function to check Palindromic Condition
function specialMatrix($matrix, $N)
{
    for ($i = 0; $i < $N; $i++)
    {
 
        // Pointer to start of row
        $start = 0;
 
        // Pointer to end of row
        $end = ($N - 1);
 
        while ($start <= $end)
        {
 
            // Single Mismatch means row is not palindromic
            if ($matrix[$i][$start] != $matrix[$i][$end])
            {
                echo "No", "\n";
                return;
            }
 
            $start++;
            $end--;
        }
    }
 
    echo "Yes", "\n";
    return;
}
 
// Driver Code
$matrix = array(array(1, 2, 1),
                array(2, 2, 2),
                array(3, 4, 3));
$N = 3;
specialMatrix($matrix, $N);
 
// This code is contributed by ajit.
?>

Javascript

<script>
    // Javascript implementation of the above approach
     
    // Function to check Palindromic Condition
    function specialMatrix(matrix, N)
    {
        for (let i = 0; i < N; i++)
        {
 
            // Pointer to start of row
            let start = 0;
 
            // Pointer to end of row
            let end = N - 1;
 
            while (start <= end)
            {
 
                // Single Mismatch means
                // row is not palindromic
                if (matrix[i][start] != matrix[i][end])
                {
                    document.write("No");
                    return;
                }
 
                start++;
                end--;
            }
        }
 
        document.write("Yes");
        return;
    }
     
    let matrix = [ [ 1, 2, 1 ],
                  [ 2, 2, 2 ],
                  [ 3, 4, 3 ] ];
    let N = 3;
    specialMatrix(matrix, N);
         
</script>
Producción: 

Yes

 

Publicación traducida automáticamente

Artículo escrito por krikti 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 *