Dada una array arr[][] de tamaño M * N , donde M es el número de filas y N es el número de columnas , y un número entero K . La tarea es barajar esta array en K pasos, de modo que en cada paso, las filas y columnas de la array se inviertan alternativamente, es decir, comience invirtiendo el primer paso de la primera fila, luego la primera columna en el segundo paso, luego la segunda fila en el tercer paso y así sucesivamente.
Nota: Si K excede la M o la N , la inversión comienza nuevamente desde la primera fila o columna.
Ejemplos:
Entrada: arr[][] = {{3, 4, 1, 8},
{11, 23, 43, 21},
{12, 17, 65, 91},
{71, 56, 34, 24}}
K = 4
Salida: {{71, 56, 4, 3},
{21, 17, 23, 12},
{11, 43, 65, 91},
{8, 1, 34, 24}}
Explicación: Pasos a seguir seguido:
Invertir la primera fila
Invertir la primera columna
Invertir la segunda fila
Invertir la segunda columnaEntrada: arr[][] = {{11, 23, 43, 21},
{12, 17, 65, 91},
{71, 56, 34, 24}}
K = 8
Salida: {{11, 43, 56, 21},
{91, 65, 17, 12},
{24, 34, 23, 71}}
Enfoque: el problema se puede resolver simplemente ejecutando dos bucles para recorrer filas y columnas alternativamente, hasta que K se convierta en 0. Al final, imprima la array resultante. Siga los pasos que se mencionan a continuación:
- Ejecute un bucle para atravesar la array.
- En cada iteración, invierta la fila y la columna adecuadas.
- Haz esta operación K veces.
- Imprime el bucle final.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ code to implement the above approach #include <bits/stdc++.h> 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
<script> // JavaScript code for the above approach let M = 3; let N = 4; // Print matrix elements function showArray(arr) { for (let i = 0; i < M; i++) { for (let j = 0; j < N; j++) document.write(arr[i][j] + " "); document.write('<br>'); } } // Function to shuffle matrix function reverseAlternate(arr, K) { let turn = 0; let row = 0, col = 0; while (turn < K) { // Reverse the row if (turn % 2 == 0) { let 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 { let 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 let matrix = [[11, 23, 43, 21], [12, 17, 65, 91], [71, 56, 34, 24]]; let K = 8; reverseAlternate(matrix, K); showArray(matrix); // This code is contributed by Potta Lokesh </script>
11 43 56 21 91 65 17 12 24 34 23 71
Complejidad de tiempo: O(K * max(M, N))
Espacio auxiliar: O(1)