📌  相关文章
📜  检查给定的两个矩阵是否互为镜像

📅  最后修改于: 2021-05-04 10:29:16             🧑  作者: Mango

给定大小为NxN的两个矩阵mat1 [] []mat2 [] [] 。任务是查找给定的两个矩阵是否彼此互为镜像。如果给定的两个矩阵是镜像,则打印“是” ,否则打印“否”

例子:

方法:该方法基于以下观察结果:仅当第一个矩阵的每一行的元素都等于另一个矩阵的反向元素行时,一个矩阵才是另一个矩阵的镜像。请按照以下步骤操作:

  • 从头到尾逐行遍历矩阵mat1 [] [],从头到尾逐行遍历mat2 [] []
  • 遍历时,如果发现mat1 [] []的任何元素不等于mat2 [] []的元素,则打印“否”
  • 遍历两个矩阵后,如果发现所有元素均相等,则打印“是”。

下面是上述方法的实现:

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


Java
// C++ implementation of
// the above approach
#include 
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);
}


Python3
// 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


C#
# 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# 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

时间复杂度: O(N 2 )
辅助空间: O(1)