检查两个给定矩阵是否互为镜像

给定两个大小为 NxN 的矩阵 mat1[][]mat2[][]。我们的任务是判断这两个给定矩阵是否互为镜像。如果是,则打印 “Yes”,否则打印 “No”。

> 如果对于矩阵的任意有效索引,大小为 N*N 的两个矩阵 mat1mat2 满足以下条件,则称它们互为镜像:

>

>

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

>

> !image

示例:

> 输入:

> mat1[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}},

> mat2[][] = {{3, 2, 1}, {5, 4, 3}, {8, 7, 6}}

> 输出: Yes

>

>

>

>

>

> 输入:

> mat1 = {{1, 2, 3}, {5, 4, 1}, {6, 7, 2}};

> mat2 = {{3, 2, 1}, {5, 4, 1}, {2, 7, 6}};

> 输出: No

方法: 这种方法基于我们的观察:一个矩阵只有在满足以下条件时才是另一个矩阵的镜像:第一个矩阵每一行的元素必须等于另一个矩阵对应行的元素逆序排列的结果。让我们按照以下步骤进行操作:

  • 从头到尾按行遍历矩阵 mat1[][],并从尾到头按行遍历 mat2[][]
  • 在遍历过程中,如果发现 mat1[][] 的任何元素与 mat2[][] 对应位置的元素不相等,则打印 "No"。
  • 遍完两个矩阵后,如果发现所有元素都相等,则打印 "Yes"。

下面是上述方法的实现:

C++

// 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 = 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 = 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)

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 = 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 Amit Katiyar

Javascript



// 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 = 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);


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/34807.html
点赞
0.00 平均评分 (0% 分数) - 0