Compruebe si las dos arrays dadas son imágenes especulares entre sí

Dadas dos arrays mat1[][] y mat2[][] de tamaño NxN . La tarea es encontrar si las dos arrays dadas son imágenes especulares entre sí. Escriba » Sí» si las dos arrays dadas son imágenes especulares, de lo contrario escriba » No» .

Se dice que dos arrays mat1 y mat2 de tamaño N*N son imágenes especulares entre sí si para cualquier índice válido (i, j) de la array: 
 

mat1[i][j] = mat2[i][N-j-1]

 

Ejemplos:  

Entrada: 
mat1[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}}, 
mat2[][] = {{3, 2, 1}, { 5, 4, 3}, {8, 7, 6}} 
Salida: Sí 
 

Entrada: 
mat1 = {{1, 2, 3}, {5, 4, 1}, {6, 7, 2}}; 
mat2 = {{3, 2, 1}, {5, 4, 1}, {2, 7, 6}}; 
Salida: Sí 
 

Enfoque: El enfoque se basa en la observación de que una array es una imagen especular de otra solo si los elementos de cada fila de la primera array son iguales a la fila invertida de elementos de la otra array. Siga los pasos a continuación:  

  • Recorra la array mat1[][] en forma de fila de principio a fin y sobre mat2[][] en forma de fila de principio a fin.
  • Durante el recorrido, si se encuentra que algún elemento de mat1[][] no es igual al elemento en mat2[][] , imprima “No”
  • Después de atravesar ambas arrays, si se encuentra que todos los elementos son iguales, imprima «Sí».

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

C++

// C++ implementation of
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether the
// two matrices are mirror
// of each other
void mirrorMatrix(int mat1[][4],
                  int mat2[][4], int N)
{
    // Initialising row and column of
    // second matrix
    int row = 0;
    int col = 0;
 
    bool isMirrorImage = true;
 
    // Iterating over the matrices
    for (int i = 0; i < N; i++) {
 
        // Check row of first matrix with
        // reversed row of second matrix
        for (int j = N - 1; j >= 0; j--) {
 
            // If the element is not equal
            if (mat2[row][col] != mat1[i][j]) {
                isMirrorImage = false;
            }
 
            // Increment column
            col++;
        }
 
        // Reset column to 0
        // for new row
        col = 0;
 
        // Increment row
        row++;
    }
 
    if (isMirrorImage)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver code
int main()
{
    // Given 2 matrices
    int N = 4;
    int mat1[][4] = { { 1, 2, 3, 4 },
                      { 0, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
 
    int mat2[][4] = { { 4, 3, 2, 1 },
                      { 8, 7, 6, 0 },
                      { 12, 11, 10, 9 },
                      { 16, 15, 14, 13 } };
 
    // Function Call
    mirrorMatrix(mat1, mat2, N);
}

Java

// Java implementation of
// the above approach
import java.util.*;
class GFG{
 
// Function to check whether the
// two matrices are mirror
// of each other
static void mirrorMatrix(int mat1[][],
                         int mat2[][], int N)
{
  // Initialising row and column of
  // second matrix
  int row = 0;
  int col = 0;
 
  boolean isMirrorImage = true;
 
  // Iterating over the matrices
  for (int i = 0; i < N; i++)
  {
    // Check row of first matrix with
    // reversed row of second matrix
    for (int j = N - 1; j >= 0; j--)
    {
      // If the element is not equal
      if (mat2[row][col] != mat1[i][j])
      {
        isMirrorImage = false;
      }
 
      // Increment column
      col++;
    }
 
    // Reset column to 0
    // for new row
    col = 0;
 
    // Increment row
    row++;
  }
 
  if (isMirrorImage)
    System.out.print("Yes");
  else
    System.out.print("No");
}
 
// Driver code
public static void main(String[] args)
{
  // Given 2 matrices
  int N = 4;
  int mat1[][] = {{1, 2, 3, 4},
                  {0, 6, 7, 8},
                  {9, 10, 11, 12},
                  {13, 14, 15, 16}};
 
  int mat2[][] = {{4, 3, 2, 1},
                  {8, 7, 6, 0},
                  {12, 11, 10, 9},
                  {16, 15, 14, 13}};
 
  // Function Call
  mirrorMatrix(mat1, mat2, N);
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 implementation of
# the above approach
 
# Function to check whether the
# two matrices are mirror
# of each other
def mirrorMatrix(mat1, mat2, N):
 
    # Initialising row and column of
    # second matrix
    row = 0
    col = 0
 
    isMirrorImage = True
 
    # Iterating over the matrices
    for i in range(N):
 
        # Check row of first matrix with
        # reversed row of second matrix
        for j in range(N - 1, -1, -1):
 
            # If the element is not equal
            if (mat2[row][col] != mat1[i][j]):
                isMirrorImage = False
 
            # Increment column
            col += 1
 
        # Reset column to 0
        # for new row
        col = 0
 
        # Increment row
        row += 1
 
    if (isMirrorImage):
        print("Yes")
    else:
        print("No")
 
# Driver code
if __name__ == '__main__':
     
    # Given 2 matrices
    N = 4
    mat1 = [ [ 1, 2, 3, 4 ],
             [ 0, 6, 7, 8 ],
             [ 9, 10, 11, 12 ],
             [ 13, 14, 15, 16 ] ]
 
    mat2 = [ [ 4, 3, 2, 1 ],
             [ 8, 7, 6, 0 ],
             [ 12, 11, 10, 9 ],
             [ 16, 15, 14, 13 ] ]
 
    # Function call
    mirrorMatrix(mat1, mat2, N)
 
# This code is contributed by mohit kumar 29

C#

// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to check whether the
// two matrices are mirror
// of each other
static void mirrorMatrix(int[,] mat1,
                         int [,]mat2, int N)
{
  // Initialising row and column of
  // second matrix
  int row = 0;
  int col = 0;
 
  bool isMirrorImage = true;
 
  // Iterating over the matrices
  for (int i = 0; i < N; i++)
  {
    // Check row of first matrix with
    // reversed row of second matrix
    for (int j = N - 1; j >= 0; j--)
    {
      // If the element is not equal
      if (mat2[row, col] != mat1[i, j])
      {
        isMirrorImage = false;
      }
 
      // Increment column
      col++;
    }
 
    // Reset column to 0
    // for new row
    col = 0;
 
    // Increment row
    row++;
  }
 
  if (isMirrorImage)
    Console.Write("Yes");
  else
    Console.Write("No");
}
 
// Driver code
public static void Main(String[] args)
{
  // Given 2 matrices
  int N = 4;
  int [,]mat1 = {{1, 2, 3, 4},
                 {0, 6, 7, 8},
                 {9, 10, 11, 12},
                 {13, 14, 15, 16}};
 
  int [,]mat2 = {{4, 3, 2, 1},
                 {8, 7, 6, 0},
                 {12, 11, 10, 9},
                 {16, 15, 14, 13}};
 
  // Function Call
  mirrorMatrix(mat1, mat2, N);
}
}
 
// This code is contributed by shikhasingrajput

Javascript

<script>
 
// Javascript implementation of
// the above approach
 
// Function to check whether the
// two matrices are mirror
// of each other
function mirrorMatrix(mat1, mat2, N)
{
    // Initialising row and column of
    // second matrix
    let row = 0;
    let col = 0;
 
    let isMirrorImage = true;
 
    // Iterating over the matrices
    for (let i = 0; i < N; i++) {
 
        // Check row of first matrix with
        // reversed row of second matrix
        for (let j = N - 1; j >= 0; j--) {
 
            // If the element is not equal
            if (mat2[row][col] != mat1[i][j]) {
                isMirrorImage = false;
            }
 
            // Increment column
            col++;
        }
 
        // Reset column to 0
        // for new row
        col = 0;
 
        // Increment row
        row++;
    }
 
    if (isMirrorImage)
        document.write("Yes");
    else
        document.write("No");
}
 
// Driver code
    // Given 2 matrices
    let N = 4;
    let mat1 = [ [ 1, 2, 3, 4 ],
                      [ 0, 6, 7, 8 ],
                      [ 9, 10, 11, 12 ],
                      [ 13, 14, 15, 16 ] ];
 
    let mat2 = [ [ 4, 3, 2, 1 ],
                      [ 8, 7, 6, 0 ],
                      [ 12, 11, 10, 9 ],
                      [ 16, 15, 14, 13 ] ];
 
    // Function Call
    mirrorMatrix(mat1, mat2, N);
 
</script>
Producción: 

Yes

 

Complejidad temporal: O(N 2
Espacio auxiliar: O(1)
 

Publicación traducida automáticamente

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