📜  检查矩阵的所有对角线是否都是回文的

📅  最后修改于: 2022-05-13 01:57:24.301000             🧑  作者: Mango

检查矩阵的所有对角线是否都是回文的

给定一个维度为N*M的矩阵mat[][] ,任务是检查矩阵的所有对角线(从右上角到左下角)是否是回文的。如果发现是真的,则打印Yes 。否则,打印No

例子:

方法:给定的 问题 可以通过执行矩阵的对角遍历来解决,并且对于每个对角遍历检查元素是否为回文。如果存在任何这样的非回文对角线,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
#define N 5
 
// Function to check if the matrix is
// palindrome or not
string isbinaryMatrixPalindrome(
    int mat[N][N])
{
 
    // Traverse the matrix and check if
    // top right and bottom left elements
    // have same value
    for (int i = 0; i < N - 1; i++) {
        for (int j = N - 1; j > i; j--) {
 
            // If top right element is not
            // equal to the bottom left
            // element return false
            if (mat[i][j] != mat[j][i]) {
                return "Np";
            }
        }
    }
 
    return "Yes";
}
 
// Driver Code
int main()
{
    int mat[N][N] = { { 1, 0, 0, 1, 1 },
                      { 0, 1, 0, 1, 0 },
                      { 0, 0, 1, 1, 1 },
                      { 1, 1, 1, 0, 1 },
                      { 1, 0, 1, 1, 0 } };
 
    cout << isbinaryMatrixPalindrome(mat);
 
    return 0;
}


Java
// Java program for the above approach
 
public class GFG {
     
    final static int N = 5;
     
    // Function to check if the matrix is
    // palindrome or not
    static String isbinaryMatrixPalindrome(int mat[][])
    {
     
        // Traverse the matrix and check if
        // top right and bottom left elements
        // have same value
        for (int i = 0; i < N - 1; i++) {
            for (int j = N - 1; j > i; j--) {
     
                // If top right element is not
                // equal to the bottom left
                // element return false
                if (mat[i][j] != mat[j][i]) {
                    return "Np";
                }
            }
        }
     
        return "Yes";
    }
     
    // Driver Code
    public static void main (String[] args) {
         
        int mat[][] = { { 1, 0, 0, 1, 1 },
                          { 0, 1, 0, 1, 0 },
                          { 0, 0, 1, 1, 1 },
                          { 1, 1, 1, 0, 1 },
                          { 1, 0, 1, 1, 0 } };
     
        System.out.println(isbinaryMatrixPalindrome(mat));
    }
}
 
// This code is contributed by AnkThon


Python3
# python program for the above approach
N = 5
 
# Function to check if the matrix is
# palindrome or not
def isbinaryMatrixPalindrome(mat):
 
    # Traverse the matrix and check if
    # top right and bottom left elements
    # have same value
    for i in range(0, N - 1):
        for j in range(N - 1, i, -1):
 
            # If top right element is not
            # equal to the bottom left
            # element return false
            if (mat[i][j] != mat[j][i]):
                return "No"
 
    return "Yes"
 
# Driver Code
if __name__ == "__main__":
 
    mat = [[1, 0, 0, 1, 1],
           [0, 1, 0, 1, 0],
           [0, 0, 1, 1, 1],
           [1, 1, 1, 0, 1],
           [1, 0, 1, 1, 0]]
    print(isbinaryMatrixPalindrome(mat))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
public class GFG {
     
    static int N = 5;
     
    // Function to check if the matrix is
    // palindrome or not
    static string isbinaryMatrixPalindrome(int [,]mat)
    {
     
        // Traverse the matrix and check if
        // top right and bottom left elements
        // have same value
        for (int i = 0; i < N - 1; i++) {
            for (int j = N - 1; j > i; j--) {
     
                // If top right element is not
                // equal to the bottom left
                // element return false
                if (mat[i, j] != mat[j, i]) {
                    return "Np";
                }
            }
        }
     
        return "Yes";
    }
     
    // Driver Code
    public static void Main (string[] args) {
         
        int [,]mat = { { 1, 0, 0, 1, 1 },
                          { 0, 1, 0, 1, 0 },
                          { 0, 0, 1, 1, 1 },
                          { 1, 1, 1, 0, 1 },
                          { 1, 0, 1, 1, 0 } };
     
        Console.WriteLine(isbinaryMatrixPalindrome(mat));
    }
}
 
// This code is contributed by ukasp.


Javascript



输出:
Yes

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