📜  双对称矩阵

📅  最后修改于: 2021-04-23 05:43:35             🧑  作者: Mango

双对称矩阵是关于两个主对角线对称的正方形矩阵。给定大小为nxn的矩阵。任务是检查其是否是双对称的。

例子 :

Input : n = 3, m[][] = { { 1, 2, 3 },
                         { 2, 5, 2 },
                         { 3, 2, 1 } };
                 
Output : Yes
Given matrix is symmetric along both the diagonal.

Input : n = 3, m[][] = { { 1, 2, 3 },
                         { 9, 5, 2 },
                         { 3, 2, 1 } };
Output : No

想法是检查给定矩阵是否沿对角线对称。对于从左上到右下的对角线,检查m [i] [j]处的元素是否等于m [j] [i]。对于从右上到左下的对角线,检查m [i] [j]处的元素是否等于m [n – j – 1] [n – i – 1]。

如果以上条件均成立,则给定矩阵为双对称矩阵,否则为非对称矩阵。

C++
// CPP Program to check if a
// given matrix is Bisymmetric
// matrix
#include 
using namespace std;
  
#define MAX 100
  
// Return if the given 
// matrix is Bisymmetric matrix
bool checkBisymmetric(int m[][MAX], 
                      int n)
{
    // Checking Across
    // Forward Diagonal
    for (int i = 0; i < n; i++) 
        for (int j = 0; j < i; j++) 
  
            // check if corresponding 
            // position element are equal.
            if (m[i][j] != m[j][i]) 
                return false;
              
  
    // Backward Diagonal
    for (int i = 0; i < n; i++) 
  
        // for each column, 
        // upto main diagonal
        for (int j = 0; j < n - i; j++) 
  
            // check if corresponding 
            // position element are equal.
            if (m[i][j] != m[n - j - 1]
                            [n - i - 1]) 
                return false;
  
    return true;
}
  
// Driven Code
int main()
{
    int n = 3;
    int m[][MAX] = { { 1, 2, 3 },
                    { 2, 5, 2 },
                    { 3, 2, 1 } };
  
    (checkBisymmetric(m, n) ? (cout << "Yes") : 
                              (cout << "No"));
    return 0;
}


Java
// Java Program to check if a given matrix
// is Bisymmetric matrix
import java.io.*;
  
class GFG {
  
    static int MAX = 100;
      
    // Return if the given 
    // matrix is Bisymmetric matrix
    static boolean checkBisymmetric(int m[][], 
                        int n)
    {
          
        // Checking Across Forward Diagonal
        for (int i = 0; i < n; i++) 
            for (int j = 0; j < i; j++) 
      
                // check if corresponding 
                // position element are equal.
                if (m[i][j] != m[j][i]) 
                    return false;
                  
        // Backward Diagonal
        for (int i = 0; i < n; i++) 
      
            // for each column, 
            // upto main diagonal
            for (int j = 0; j < n - i; j++) 
      
                // check if corresponding 
                // position element are equal.
                if (m[i][j] != m[n - j - 1]
                                [n - i - 1]) 
                    return false;
      
        return true;
    }
      
    // Driven Code
    public static void main (String[] args)
    {
        int n = 3;
          
        int m[][] = { { 1, 2, 3 },
                      { 2, 5, 2 },
                      { 3, 2, 1 } };
  
        if(checkBisymmetric(m, n))
            System.out.println( "Yes");
        else
            System.out.println( "No");
    }
}
  
// This code is contributed by anuj_67.


Python3
# Python3 Program to check if a
# given matrix is Bisymmetric
# matrix
  
# Return if the given matrix
# is Bisymmetric matrix
def checkBisymmetric(m, n) :
      
    # Checking Across 
    # Forward Diagonal
    for i in range(0, n) :
        for j in range(0, i) : 
            # check if corresponding 
            # position element are equal.
            if (m[i][j] != m[j][i]) : 
                return false             
  
    # Backward Diagonal
    for i in range(0, n) : 
        # for each column, 
        # upto main diagonal
        for j in range(0, n - i) : 
              
            # check if corresponding 
            # position element are 
            # equal.
            if (m[i][j] != 
             m[n - j - 1][n - i - 1]) :
                return False
    return True;
  
# Driven Code
n = 3;
m = [[ 1, 2, 3 ],
     [ 2, 5, 2 ],
     [ 3, 2, 1 ]]
  
if(checkBisymmetric(m, n)) :
    print ("Yes") 
else :
    print ("No")
  
# This code is contributed by 
# Manish Shaw (manishshaw1)


C#
// C# Program to check if a given matrix
// is Bisymmetric matrix
using System;
  
class GFG {
  
    //static int MAX = 100;
      
    // Return if the given 
    // matrix is Bisymmetric matrix
    static bool checkBisymmetric(int [,]m, 
                        int n)
    {
          
        // Checking Across Forward Diagonal
        for (int i = 0; i < n; i++) 
            for (int j = 0; j < i; j++) 
      
                // check if corresponding 
                // position element are equal.
                if (m[i,j] != m[j,i]) 
                    return false;
                  
        // Backward Diagonal
        for (int i = 0; i < n; i++) 
      
            // for each column, 
            // upto main diagonal
            for (int j = 0; j < n - i; j++) 
      
                // check if corresponding 
                // position element are equal.
                if (m[i,j] != m[n - j - 1,
                                n - i - 1]) 
                    return false;
      
        return true;
    }
      
    // Driven Code
    public static void Main ()
    {
        int n = 3;
          
        int [,]m = { { 1, 2, 3 },
                    { 2, 5, 2 },
                    { 3, 2, 1 } };
  
        if(checkBisymmetric(m, n))
            Console.WriteLine( "Yes");
        else
            Console.WriteLine( "No");
    }
}
  
// This code is contributed by anuj_67.


PHP


输出 :
Yes