📜  矩阵对角线镜像的Java程序

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

矩阵对角线镜像的Java程序

给定一个 N x N 阶的二维数组,打印一个矩阵,它是给定树在对角线上的镜像。我们需要以某种方式打印结果:将对角线上方的三角形的值与其下方的三角形的值交换,就像镜像交换一样。打印以矩阵布局获得的二维数组。

例子:

Input : int mat[][] = {{1 2 4 }
                       {5 9 0}
                       { 3 1 7}}
Output :  1 5 3 
          2 9 1
          4 0 7

Input : mat[][] = {{1  2  3  4 }
                   {5  6  7  8 }
                   {9  10 11 12}
                   {13 14 15 16} }
Output : 1 5 9 13 
         2 6 10 14  
         3 7 11 15 
         4 8 12 16 

这个问题的一个简单解决方案涉及额外的空间。我们一一遍历所有右对角线(从右到左)。在遍历对角线的过程中,首先我们将所有元素压入栈中,然后再次遍历它,并将对角线的每个元素替换为栈元素。

下面是上述思想的实现。

Java
// Simple Java program to find mirror of
// matrix across diagonal.
  
import java.util.*;
  
class GFG 
{
  
    static int MAX = 100;
  
    static void imageSwap(int mat[][], int n) 
    {
        // for diagonal which start from at 
        // first row of matrix
        int row = 0;
  
        // traverse all top right diagonal
        for (int j = 0; j < n; j++) 
        {
  
            // here we use stack for reversing
            // the element of diagonal
            Stack s = new Stack<>();
            int i = row, k = j;
            while (i < n && k >= 0)
            {
                s.push(mat[i++][k--]);
            }
  
            // push all element back to matrix 
            // in reverse order
            i = row;
            k = j;
            while (i < n && k >= 0)
            {
                mat[i++][k--] = s.peek();
                s.pop();
            }
        }
  
        // do the same process for all the
        // diagonal which start from last
        // column
        int column = n - 1;
        for (int j = 1; j < n; j++)
        {
  
            // here we use stack for reversing 
            // the elements of diagonal
            Stack s = new Stack<>();
            int i = j, k = column;
            while (i < n && k >= 0)
            {
                s.push(mat[i++][k--]);
            }
  
            // push all element back to matrix 
            // in reverse order
            i = j;
            k = column;
            while (i < n && k >= 0)
            {
                mat[i++][k--] = s.peek();
                s.pop();
            }
        }
    }
  
    // Utility function to print a matrix
    static void printMatrix(int mat[][], int n) 
    {
        for (int i = 0; i < n; i++) 
        {
            for (int j = 0; j < n; j++)
            {
                System.out.print(mat[i][j] + " ");
            }
            System.out.println("");
        }
    }
  
    // Driver program to test above function
    public static void main(String[] args)
    {
  
        int mat[][] = {{1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}};
        int n = 4;
        imageSwap(mat, n);
        printMatrix(mat, n);
    }
}
  
// This code contributed by Rajput-Ji


Java
// Efficient Java program to find mirror of
// matrix across diagonal.
import java.io.*;
  
class GFG {
      
    static int MAX = 100;
      
    static void imageSwap(int mat[][], int n)
    {
          
        // traverse a matrix and swap 
        // mat[i][j] with mat[j][i]
        for (int i = 0; i < n; i++)
            for (int j = 0; j <= i; j++) 
                mat[i][j] = mat[i][j] + mat[j][i]
                       - (mat[j][i] = mat[i][j]);     
    }
      
    // Utility function to print a matrix
    static void printMatrix(int mat[][], int n)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                System.out.print( mat[i][j] + " ");
            System.out.println();
        }
    }
      
    // driver program to test above function
    public static void main (String[] args) 
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };
        int n = 4;
        imageSwap(mat, n);
        printMatrix(mat, n);
    }
}
  
// This code is contributed by anuj_67.


输出:

1 5 9 13 
2 6 10 14 
3 7 11 15 
4 8 12 16

时间复杂度: O(n*n)

这个问题的一个有效解决方案是,如果我们观察一个输出矩阵,那么我们注意到我们只需要交换 (mat[i][j] 到 mat[j][i])。
下面是上述思想的实现。

Java

// Efficient Java program to find mirror of
// matrix across diagonal.
import java.io.*;
  
class GFG {
      
    static int MAX = 100;
      
    static void imageSwap(int mat[][], int n)
    {
          
        // traverse a matrix and swap 
        // mat[i][j] with mat[j][i]
        for (int i = 0; i < n; i++)
            for (int j = 0; j <= i; j++) 
                mat[i][j] = mat[i][j] + mat[j][i]
                       - (mat[j][i] = mat[i][j]);     
    }
      
    // Utility function to print a matrix
    static void printMatrix(int mat[][], int n)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                System.out.print( mat[i][j] + " ");
            System.out.println();
        }
    }
      
    // driver program to test above function
    public static void main (String[] args) 
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 },
                        { 13, 14, 15, 16 } };
        int n = 4;
        imageSwap(mat, n);
        printMatrix(mat, n);
    }
}
  
// This code is contributed by anuj_67.

输出:

1 5 9 13 
2 6 10 14 
3 7 11 15 
4 8 12 16 

时间复杂度: O(n*n)

有关详细信息,请参阅关于矩阵对角线镜像的完整文章!