矩阵的垂直对称性

给定一个二维二进制矩阵 N 一排 M 柱。任务是检查矩阵是水平对称、垂直对称还是两者都对称。如果第一行与最后一行相同,第二行与最后第二行相同,则称矩阵为水平对称矩阵,依此类推。如果第一列与最后一列相同,第二列与最后一列相同,那么矩阵称为垂直对称,依此类推。如果矩阵垂直对称,打印“垂直”,如果矩阵垂直对称,打印“水平”,如果矩阵垂直和水平对称,打印“两者”,如果矩阵不对称,打印“否”。

null

例如:

Input: N = 3 M = 30 1 00 0 00 1 0Output: BothFirst and third row are same and also second row is in middle. So Horizontal Symmetric.Similarly, First and third column are same andalso second column is in middle, so Vertical Symmetric.Input:0 0 11 1 00 0 1.Output: Both 

其思想是使用指示两行(或两列)的指针,并比较两个指定行(或两列)的每个单元格。 对于水平对称,初始化一个指针i=0,另一个指针j=N–1。 现在,比较第i行和第j行的每个元素。在每个循环中,增加i 1,减少j 1。如果至少找到一个元素,而不是相同的元素,则将矩阵标记为非水平对称。 同样,对于垂直对称,初始化一个指针i=0,另一个指针j=M–1。 现在,比较第i列和第j列的每个元素。在每个循环中,增加i 1,减少j 1。如果至少找到一个元素,而不是相同的元素,则将矩阵标记为非垂直对称。

以下是上述理念的实施情况:

C++

// C++ program to find if a matrix is symmetric.
#include <bits/stdc++.h>
#define MAX 1000
using namespace std;
void checkHV( int arr[][MAX], int N, int M)
{
// Initializing as both horizontal and vertical
// symmetric.
bool horizontal = true , vertical = true ;
// Checking for Horizontal Symmetry.  We compare
// first row with last row, second row with second
// last row and so on.
for ( int i = 0, k = N - 1; i < N / 2; i++, k--) {
// Checking each cell of a column.
for ( int j = 0; j < M; j++) {
// check if every cell is identical
if (arr[i][j] != arr[k][j]) {
horizontal = false ;
break ;
}
}
}
// Checking for Vertical Symmetry.  We compare
// first column with last column, second column
// with second last column and so on.
for ( int i = 0, k = M - 1; i < M / 2; i++, k--) {
// Checking each cell of a row.
for ( int j = 0; j < N; j++) {
// check if every cell is identical
if (arr[i][j] != arr[k][j]) {
vertical = false ;
break ;
}
}
}
if (!horizontal && !vertical)
cout << "NO" ;
else if (horizontal && !vertical)
cout << "HORIZONTAL" ;
else if (vertical && !horizontal)
cout << "VERTICAL" ;
else
cout << "BOTH" ;
}
// Driven Program
int main()
{
int mat[MAX][MAX] = { { 1, 0, 1 },
{ 0, 0, 0 },
{ 1, 0, 1 } };
checkHV(mat, 3, 3);
return 0;
}


JAVA

// Java program to find if
// a matrix is symmetric.
import java.io.*;
public class GFG {
static void checkHV( int [][] arr, int N,
int M)
{
// Initializing as both horizontal
// and vertical symmetric.
boolean horizontal = true ;
boolean vertical = true ;
// Checking for Horizontal Symmetry.
// We compare first row with last
// row, second row with second
// last row and so on.
for ( int i = 0 , k = N - 1 ;
i < N / 2 ; i++, k--) {
// Checking each cell of a column.
for ( int j = 0 ; j < M; j++) {
// check if every cell is identical
if (arr[i][j] != arr[k][j]) {
horizontal = false ;
break ;
}
}
}
// Checking for Vertical Symmetry. We compare
// first column with last column, second column
// with second last column and so on.
for ( int i = 0 , k = M - 1 ;
i < M / 2 ; i++, k--) {
// Checking each cell of a row.
for ( int j = 0 ; j < N; j++) {
// check if every cell is identical
if (arr[i][j] != arr[k][j]) {
horizontal = false ;
break ;
}
}
}
if (!horizontal && !vertical)
System.out.println( "NO" );
else if (horizontal && !vertical)
System.out.println( "HORIZONTAL" );
else if (vertical && !horizontal)
System.out.println( "VERTICAL" );
else
System.out.println( "BOTH" );
}
// Driver Code
static public void main(String[] args)
{
int [][] mat = { { 1 , 0 , 1 },
{ 0 , 0 , 0 },
{ 1 , 0 , 1 } };
checkHV(mat, 3 , 3 );
}
}
// This code is contributed by vt_m.


Python3

# Python3 program to find if a matrix is symmetric.
MAX = 1000
def checkHV(arr, N, M):
# Initializing as both horizontal and vertical
# symmetric.
horizontal = True
vertical = True
# Checking for Horizontal Symmetry. We compare
# first row with last row, second row with second
# last row and so on.
i = 0
k = N - 1
while (i < N / / 2 ):
# Checking each cell of a column.
for j in range (M):
# check if every cell is identical
if (arr[i][j] ! = arr[k][j]):
horizontal = False
break
i + = 1
k - = 1
# Checking for Vertical Symmetry. We compare
# first column with last column, second column
# with second last column and so on.
i = 0
k = M - 1
while (i < M / / 2 ):
# Checking each cell of a row.
for j in range (N):
# check if every cell is identical
if (arr[i][j] ! = arr[k][j]):
vertical = False
break
i + = 1
k - = 1
if ( not horizontal and not vertical):
print ( "NO" )
elif (horizontal and not vertical):
print ( "HORIZONTAL" )
elif (vertical and not horizontal):
print ( "VERTICAL" )
else :
print ( "BOTH" )
# Driver code
mat = [[ 1 , 0 , 1 ],[ 0 , 0 , 0 ],[ 1 , 0 , 1 ]]
checkHV(mat, 3 , 3 )
# This code is contributed by shubhamsingh10


C#

// C# program to find if
// a matrix is symmetric.
using System;
public class GFG {
static void checkHV( int [, ] arr, int N,
int M)
{
// Initializing as both horizontal
// and vertical symmetric.
bool horizontal = true ;
bool vertical = true ;
// Checking for Horizontal Symmetry.
// We compare first row with last
// row, second row with second
// last row and so on.
for ( int i = 0, k = N - 1;
i < N / 2; i++, k--) {
// Checking each cell of a column.
for ( int j = 0; j < M; j++) {
// check if every cell is identical
if (arr[i, j] != arr[k, j]) {
horizontal = false ;
break ;
}
}
}
// Checking for Vertical Symmetry. We compare
// first column with last column, second column
// with second last column and so on.
for ( int i = 0, k = M - 1;
i < M / 2; i++, k--) {
// Checking each cell of a row.
for ( int j = 0; j < N; j++) {
// check if every cell is identical
if (arr[i, j] != arr[k, j]) {
horizontal = false ;
break ;
}
}
}
if (!horizontal && !vertical)
Console.WriteLine( "NO" );
else if (horizontal && !vertical)
Console.WriteLine( "HORIZONTAL" );
else if (vertical && !horizontal)
Console.WriteLine( "VERTICAL" );
else
Console.WriteLine( "BOTH" );
}
// Driver Code
static public void Main()
{
int [, ] mat = { { 1, 0, 1 },
{ 0, 0, 0 },
{ 1, 0, 1 } };
checkHV(mat, 3, 3);
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP program to find if
// a matrix is symmetric.
function checkHV( $arr , $N , $M )
{
// Initializing as both horizontal
// and vertical symmetric.
$horizontal = true; $vertical = true;
// Checking for Horizontal Symmetry.
// We compare first row with last row,
// second row with second last row
// and so on.
for ( $i = 0, $k = $N - 1;
$i < $N / 2; $i ++,
$k --)
{
// Checking each cell of a column.
for ( $j = 0; $j < $M ; $j ++)
{
// check if every cell is identical
if ( $arr [ $i ][ $j ] != $arr [ $k ][ $j ])
{
$horizontal = false;
break ;
}
}
}
// Checking for Vertical Symmetry.
// We compare first column with
// last column, second column with
// second last column and so on.
for ( $i = 0, $k = $M - 1;
$i < $M / 2; $i ++,
$k --)
{
// Checking each cell of a row.
for ( $j = 0; $j < $N ; $j ++)
{
// check if every cell is identical
if ( $arr [ $i ][ $j ] != $arr [ $k ][ $j ])
{
$horizontal = false;
break ;
}
}
}
if (! $horizontal && ! $vertical )
echo "NO" ;
else if ( $horizontal && ! $vertical )
cout << "HORIZONTAL" ;
else if ( $vertical && ! $horizontal )
echo "VERTICAL" ;
else echo "BOTH" ;
}
// Driver Code
$mat = array ( array (1, 0, 1),
array (0, 0, 0),
array (1, 0, 1));
checkHV( $mat , 3, 3);
// This code is contributed by nitin mittal.
?>


Javascript

<script>
// Javascript program to find if
// a matrix is symmetric.
function checkHV(arr, N, M)
{
// Initializing as both horizontal
// and vertical symmetric.
let horizontal = true ;
let vertical = true ;
// Checking for Horizontal Symmetry.
// We compare first row with last
// row, second row with second
// last row and so on.
for (let i = 0, k = N - 1;
i < parseInt(N / 2, 10); i++, k--) {
// Checking each cell of a column.
for (let j = 0; j < M; j++) {
// check if every cell is identical
if (arr[i][j] != arr[k][j]) {
horizontal = false ;
break ;
}
}
}
// Checking for Vertical Symmetry. We compare
// first column with last column, second column
// with second last column and so on.
for (let i = 0, k = M - 1;
i < parseInt(M / 2, 10); i++, k--) {
// Checking each cell of a row.
for (let j = 0; j < N; j++) {
// check if every cell is identical
if (arr[i][j] != arr[k][j]) {
horizontal = false ;
break ;
}
}
}
if (!horizontal && !vertical)
document.write( "NO" );
else if (horizontal && !vertical)
document.write( "HORIZONTAL" );
else if (vertical && !horizontal)
document.write( "VERTICAL" );
else
document.write( "BOTH" );
}
let mat = [ [ 1, 0, 1 ],
[ 0, 0, 0 ],
[ 1, 0, 1 ] ];
checkHV(mat, 3, 3);
</script>


输出:

BOTH

时间复杂性: O(N*M)。

本文由 阿努伊·乔汉 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享