📌  相关文章
📜  通过按顺序交替颠倒行和列来将给定的矩阵洗牌 K 次

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

通过按顺序交替颠倒行和列来将给定的矩阵洗牌 K 次

给定一个大小为M * N的矩阵arr[][] ,其中M数, N数,以及一个整数K。任务是在K步中对该矩阵进行混洗,以便在每一步中,矩阵的行和列交替反转,即从第一步反转第一行开始,然后在第二步中反转第一列,然后在第二步中反转第二行第三步以此类推。

注意:如果K超过MN ,则反转再次从第一行或第一列开始。

例子:

方法:这个问题可以通过简单地运行两个循环来交替遍历行和列来解决,直到K变为0。最后,打印结果矩阵。请按照以下步骤操作:

  • 运行一个循环遍历矩阵。
  • 在每次迭代中反转正确的行和列。
  • 执行此操作 K 次。
  • 打印最后一个循环。

下面是上述方法的实现

C++
// C++ code to implement the above approach
#include 
using namespace std;
const int M = 3;
const int N = 4;
 
// Print matrix elements
void showArray(int arr[][N])
{
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++)
            cout << arr[i][j] << " ";
        cout << endl;
    }
}
 
// Function to shuffle matrix
void reverseAlternate(int arr[][N], int K)
{
    int turn = 0;
    int row = 0, col = 0;
    while (turn < K) {
         
        // Reverse the row
        if (turn % 2 == 0) {
            int start = 0, end = N - 1, temp;
            while (start < end) {
                temp = arr[row % M][start];
                arr[row % M][start] =
                    arr[row % M][end];
                arr[row % M][end] = temp;
                start += 1;
                end -= 1;
            }
            row++;
            turn++;
        }
         
        // Reverse the column
        else {
            int start = 0, end = M - 1, temp;
            while (start < end) {
                temp = arr[start][col % N];
                arr[start][col % N] =
                    arr[end][col % N];
                arr[end][col % N] = temp;
                start += 1;
                end -= 1;
            }
            col++;
            turn++;
        }
    }
}
 
// Driver code
int main()
{
 
    int matrix[M][N] = { { 11, 23, 43, 21 },
                         { 12, 17, 65, 91 },
                         { 71, 56, 34, 24 } };
    int K = 8;
    reverseAlternate(matrix, K);
    showArray(matrix);
    return 0;
}


Java
// Java code to implement the above approach
import java.io.*;
 
class GFG {
 
  public static int M = 3;
  public static int N = 4;
 
  // Print matrix elements
  public static void showArray(int arr[][])
  {
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++)
        System.out.print(arr[i][j] + " ");
      System.out.println();
    }
  }
 
  // Function to shuffle matrix
  public static void reverseAlternate(int arr[][], int K)
  {
    int turn = 0;
    int row = 0, col = 0;
    while (turn < K) {
 
      // Reverse the row
      if (turn % 2 == 0) {
        int start = 0, end = N - 1, temp;
        while (start < end) {
          temp = arr[row % M][start];
          arr[row % M][start] =
            arr[row % M][end];
          arr[row % M][end] = temp;
          start += 1;
          end -= 1;
        }
        row++;
        turn++;
      }
 
      // Reverse the column
      else {
        int start = 0, end = M - 1, temp;
        while (start < end) {
          temp = arr[start][col % N];
          arr[start][col % N] =
            arr[end][col % N];
          arr[end][col % N] = temp;
          start += 1;
          end -= 1;
        }
        col++;
        turn++;
      }
    }
  }
 
  // Driver code
  public static void main (String[] args)
  {
 
    int matrix[][] = { { 11, 23, 43, 21 },
                      { 12, 17, 65, 91 },
                      { 71, 56, 34, 24 } };
    int K = 8;
    reverseAlternate(matrix, K);
    showArray(matrix);
  }
}
 
// This code is contributed by Shubham Singh


Python3
# Python code to implement the above approach
M = 3
N = 4
 
# Print matrix elements
def showArray(arr):
     
    for i in range(M):
        for j in range(N):
            print(arr[i][j], end= " ")
        print()
 
# Function to shuffle matrix
def reverseAlternate(arr, K):
     
    turn = 0
    row = 0
    col = 0
    while (turn < K):
         
        # Reverse the row
        if (turn % 2 == 0):
            start = 0
            end = N - 1
            temp = 0
            while (start < end):
                temp = arr[row % M][start]
                arr[row % M][start] = arr[row % M][end]
                arr[row % M][end] = temp
                start += 1
                end -= 1
             
            row += 1
            turn += 1
         
        #Reverse the column
        else:
            start = 0
            end = M - 1
            temp = 0
            while (start < end):
                temp = arr[start][col % N]
                arr[start][col % N] = arr[end][col % N]
                arr[end][col % N] = temp
                start += 1
                end -= 1
                 
            col += 1
            turn += 1
             
# Driver code
matrix = [[11, 23, 43, 21] ,
            [12, 17, 65, 91] ,
            [71, 56, 34, 24]]
K = 8
reverseAlternate(matrix, K)
showArray(matrix)
         
# This code is contributed by Shubham Singh


C#
// C# code to implement the above approach
using System;
 
class GFG {
 
  public static int M = 3;
  public static int N = 4;
 
  // Print matrix elements
  public static void showArray(int[, ] arr)
  {
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++)
        Console.Write(arr[i, j] + " ");
      Console.WriteLine();
    }
  }
 
  // Function to shuffle matrix
  public static void reverseAlternate(int[, ] arr, int K)
  {
    int turn = 0;
    int row = 0, col = 0;
    while (turn < K) {
 
      // Reverse the row
      if (turn % 2 == 0) {
        int start = 0, end = N - 1, temp;
        while (start < end) {
          temp = arr[row % M, start];
          arr[row % M, start] = arr[row % M, end];
          arr[row % M, end] = temp;
          start += 1;
          end -= 1;
        }
        row++;
        turn++;
      }
 
      // Reverse the column
      else {
        int start = 0, end = M - 1, temp;
        while (start < end) {
          temp = arr[start, col % N];
          arr[start, col % N] = arr[end, col % N];
          arr[end, col % N] = temp;
          start += 1;
          end -= 1;
        }
        col++;
        turn++;
      }
    }
  }
 
  // Driver code
  public static void Main(string[] args)
  {
 
    int[, ] matrix = { { 11, 23, 43, 21 },
                      { 12, 17, 65, 91 },
                      { 71, 56, 34, 24 } };
    int K = 8;
    reverseAlternate(matrix, K);
    showArray(matrix);
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
11 43 56 21 
91 65 17 12 
24 34 23 71 

时间复杂度: O(K * max(M, N))
辅助空间: O(1)