给定两个大小为NxN 的矩阵mat1[][]和mat2[][] 。任务是找出给定的两个矩阵是否是彼此的镜像。如果给定的两个矩阵是镜像,则打印“是” ,否则打印“否” 。
Two matrices mat1 and mat2 of size N*N are said to be mirror images of one another if for any valid index (i, j) of the matrix:
例子:
Input:
mat1[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}},
mat2[][] = {{3, 2, 1}, {5, 4, 3}, {8, 7, 6}}
Output: Yes
Input:
mat1 = {{1, 2, 3}, {5, 4, 1}, {6, 7, 2}};
mat2 = {{3, 2, 1}, {5, 4, 1}, {2, 7, 6}};
Output: No
方法:该方法基于以下观察:只有当第一个矩阵的每一行的元素等于另一个矩阵的元素的反向行时,矩阵才是另一个矩阵的镜像。请按照以下步骤操作:
- 从开始到结束逐行遍历矩阵mat1[][]并从结束到开始逐行遍历mat2[][] 。
- 遍历时,如果发现mat1[][] 的任何元素不等于mat2[][]的元素,则打印“No”
- 遍历两个矩阵后,如果发现所有元素都相等,则打印“Yes”。
下面是上述方法的实现:
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
Javascript
// 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)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live