📌  相关文章
📜  将除对角线以外的所有 Matrix 元素顺时针旋转 90 度

📅  最后修改于: 2021-09-02 07:30:57             🧑  作者: Mango

给定一个维度为N 的方阵mat[][]和一个整数K ,任务是将矩阵旋转 90 度K次而不改变对角线元素的位置。

例子:

方法:利用本文讨论的思想和矩阵顺时针旋转4次后恢复的事实可以解决给定的问题。按照以下步骤解决给定的问题:

  • K的值更新为K % 4
  • 迭代直到K为正数并执行以下步骤:
    • 穿过所述基质,对于i超过在范围[0的范围[0,N / 2)j,N – I – 1),并执行以下步骤:
    • 如果i != j(i + j) != (N – 1) 的值,则执行以下步骤:
      • mat[i][j]的值存储在临时变量temp 中
      • mat[i][j]的值更新为mat[N – 1 – j][i]
      • mat[N – 1 – j][i]的值更新为mat[N – 1 -i][N – 1 – j]
      • mat[N – 1 – i][N – 1 – j]的值更新为mat[j][N – 1 – i]
      • mat[j][N – 1 – i]的值更新为temp
  • 完成上述步骤后,打印得到的更新矩阵。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to print the matrix
void print(vector >& mat)
{
    // Iterate over the rows
    for (int i = 0; i < mat.size(); i++) {
 
        // Iterate over the columns
        for (int j = 0; j < mat[0].size(); j++)
 
            // Print the value
            cout << setw(3) << mat[i][j];
        cout << "\n";
    }
}
 
// Function to perform the swapping of
// matrix elements in clockwise manner
void performSwap(vector >& mat,
                 int i, int j)
{
    int N = mat.size();
 
    // Stores the last row
    int ei = N - 1 - i;
 
    // Stores the last column
    int ej = N - 1 - j;
 
    // Perform the swaps
    int temp = mat[i][j];
    mat[i][j] = mat[ej][i];
    mat[ej][i] = mat[ei][ej];
    mat[ei][ej] = mat[j][ei];
    mat[j][ei] = temp;
}
 
// Function to rotate non - diagonal
// elements of the matrix K times in
// clockwise direction
void rotate(vector >& mat,
            int N, int K)
{
    // Update K to K % 4
    K = K % 4;
 
    // Iterate until K is positive
    while (K--) {
 
        // Iterate each up to N/2-th row
        for (int i = 0; i < N / 2; i++) {
 
            // Iterate each column
            // from i to N - i - 1
            for (int j = i;
                 j < N - i - 1; j++) {
 
                // Check if the element
                // at i, j is not a
                // diagonal element
                if (i != j
                    && (i + j) != N - 1) {
 
                    // Perform the swapping
                    performSwap(mat, i, j);
                }
            }
        }
    }
 
    // Print the matrix
    print(mat);
}
 
// Driver Code
int main()
{
    int K = 5;
    vector > mat = {
        { 1, 2, 3, 4 },
        { 6, 7, 8, 9 },
        { 11, 12, 13, 14 },
        { 16, 17, 18, 19 },
    };
    int N = mat.size();
    rotate(mat, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to print the matrix
    static void print(int mat[][])
    {
        // Iterate over the rows
        for (int i = 0; i < mat.length; i++) {
 
            // Iterate over the columns
            for (int j = 0; j < mat[0].length; j++)
 
                // Print the value
                System.out.print(mat[i][j] + " ");
           
            System.out.println();
        }
    }
 
    // Function to perform the swapping of
    // matrix elements in clockwise manner
    static void performSwap(int mat[][], int i, int j)
    {
        int N = mat.length;
 
        // Stores the last row
        int ei = N - 1 - i;
 
        // Stores the last column
        int ej = N - 1 - j;
 
        // Perform the swaps
        int temp = mat[i][j];
        mat[i][j] = mat[ej][i];
        mat[ej][i] = mat[ei][ej];
        mat[ei][ej] = mat[j][ei];
        mat[j][ei] = temp;
    }
 
    // Function to rotate non - diagonal
    // elements of the matrix K times in
    // clockwise direction
    static void rotate(int mat[][], int N, int K)
    {
        // Update K to K % 4
        K = K % 4;
 
        // Iterate until K is positive
        while (K-- > 0) {
 
            // Iterate each up to N/2-th row
            for (int i = 0; i < N / 2; i++) {
 
                // Iterate each column
                // from i to N - i - 1
                for (int j = i; j < N - i - 1; j++) {
 
                    // Check if the element
                    // at i, j is not a
                    // diagonal element
                    if (i != j && (i + j) != N - 1) {
 
                        // Perform the swapping
                        performSwap(mat, i, j);
                    }
                }
            }
        }
 
        // Print the matrix
        print(mat);
    }
   
    // Driver Code
    public static void main(String[] args)
    {
 
        int K = 5;
        int mat[][] = {
            { 1, 2, 3, 4 },
            { 6, 7, 8, 9 },
            { 11, 12, 13, 14 },
            { 16, 17, 18, 19 },
        };
       
        int N = mat.length;
        rotate(mat, N, K);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
# Function to print the matrix
def printMat(mat):
   
    # Iterate over the rows
    for i in range(len(mat)):
 
        # Iterate over the columns
        for j in range(len(mat[0])):
 
            # Print the value
            print(mat[i][j], end = " ")
             
        print()
 
# Function to perform the swapping of
# matrix elements in clockwise manner
def performSwap(mat, i, j):
   
    N = len(mat)
 
    # Stores the last row
    ei = N - 1 - i
 
    # Stores the last column
    ej = N - 1 - j
 
    # Perform the swaps
    temp = mat[i][j]
    mat[i][j] = mat[ej][i]
    mat[ej][i] = mat[ei][ej]
    mat[ei][ej] = mat[j][ei]
    mat[j][ei] = temp
 
# Function to rotate non - diagonal
# elements of the matrix K times in
# clockwise direction
def rotate(mat, N, K):
 
    # Update K to K % 4
    K = K % 4
 
    # Iterate until K is positive
    while (K > 0):
 
        # Iterate each up to N/2-th row
        for i in range(int(N / 2)):
 
            # Iterate each column
            # from i to N - i - 1
            for j in range(i, N - i - 1):
 
                # Check if the element
                # at i, j is not a
                # diagonal element
                if (i != j and (i + j) != N - 1):
 
                    # Perform the swapping
                    performSwap(mat, i, j)
                     
        K -= 1
                     
    # Print the matrix
    printMat(mat)
 
# Driver Code
K = 5
mat = [ [ 1, 2, 3, 4 ],
        [ 6, 7, 8, 9 ],
        [ 11, 12, 13, 14 ],
        [ 16, 17, 18, 19 ] ]
N = len(mat)
 
rotate(mat, N, K)
 
# This code is contributed by Dharanendra L V.


C#
// C# program for the above approach
using System;
public class GFG {
 
    // Function to print the matrix
    static void print(int[, ] mat)
    {
        // Iterate over the rows
        for (int i = 0; i < mat.GetLength(0); i++) {
 
            // Iterate over the columns
            for (int j = 0; j < mat.GetLength(1); j++)
 
                // Print the value
                Console.Write(mat[i, j] + " ");
 
            Console.WriteLine();
        }
    }
 
    // Function to perform the swapping of
    // matrix elements in clockwise manner
    static void performSwap(int[, ] mat, int i, int j)
    {
        int N = mat.GetLength(0);
 
        // Stores the last row
        int ei = N - 1 - i;
 
        // Stores the last column
        int ej = N - 1 - j;
 
        // Perform the swaps
        int temp = mat[i, j];
        mat[i, j] = mat[ej, i];
        mat[ej, i] = mat[ei, ej];
        mat[ei, ej] = mat[j, ei];
        mat[j, ei] = temp;
    }
 
    // Function to rotate non - diagonal
    // elements of the matrix K times in
    // clockwise direction
    static void rotate(int[, ] mat, int N, int K)
    {
        // Update K to K % 4
        K = K % 4;
 
        // Iterate until K is positive
        while (K-- > 0) {
 
            // Iterate each up to N/2-th row
            for (int i = 0; i < N / 2; i++) {
 
                // Iterate each column
                // from i to N - i - 1
                for (int j = i; j < N - i - 1; j++) {
 
                    // Check if the element
                    // at i, j is not a
                    // diagonal element
                    if (i != j && (i + j) != N - 1) {
 
                        // Perform the swapping
                        performSwap(mat, i, j);
                    }
                }
            }
        }
 
        // Print the matrix
        print(mat);
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
 
        int K = 5;
        int[, ] mat = {
            { 1, 2, 3, 4 },
            { 6, 7, 8, 9 },
            { 11, 12, 13, 14 },
            { 16, 17, 18, 19 },
        };
 
        int N = mat.GetLength(0);
        rotate(mat, N, K);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
1 11  6  4
 17  7  8  2
 18 12 13  3
 16 14  9 19

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live