Dada una array 2D, imprímala en forma de espiral inversa. Ya hemos discutido Imprimir una array dada en forma de espiral . Este artículo explica cómo hacer la impresión inversa. Vea los siguientes ejemplos.
Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output: 11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1
Python3
# Python3 Code to Print a given # matrix in reverse spiral form # This is a modified code of # https:#www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/ R, C = 3, 6 def ReversespiralPrint(m, n, a): # Large array to initialize it # with elements of matrix b = [0 for i in range(100)] #/* k - starting row index #l - starting column index*/ i, k, l = 0, 0, 0 # Counter for single dimension array # in which elements will be stored z = 0 # Total elements in matrix size = m * n while (k < m and l < n): # Variable to store value of matrix. val = 0 # Print the first row # from the remaining rows for i in range(l, n): # printf("%d ", a[k][i]) val = a[k][i] b[z] = val z += 1 k += 1 # Print the last column # from the remaining columns for i in range(k, m): # printf("%d ", a[i][n-1]) val = a[i][n - 1] b[z] = val z += 1 n -= 1 # Print the last row # from the remaining rows if (k < m): for i in range(n - 1, l - 1, -1): # printf("%d ", a[m-1][i]) val = a[m - 1][i] b[z] = val z += 1 m -= 1 # Print the first column # from the remaining columns if (l < n): for i in range(m - 1, k - 1, -1): # printf("%d ", a[i][l]) val = a[i][l] b[z] = val z += 1 l += 1 for i in range(size - 1, -1, -1): print(b[i], end = " ") # Driver Code a = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18]] ReversespiralPrint(R, C, a) # This code is contributed by mohit kumar
Producción:
11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1
Complejidad temporal : O(m*n) donde m es el número de filas y n es el número de columnas de una array dada
Consulte el artículo completo sobre Imprimir una array determinada en forma de espiral inversa para obtener más detalles.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA