Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Problem Statement

You are given a 2D matrix, a, of dimension \$MxN\$ and a positive integer \$R\$. You have to rotate the matrix R times and print the resultant matrix. Rotation should be in anti-clockwise direction.

Rotation of a 4x5 matrix is represented by the following figure. Note that in one rotation, you have to shift elements by one step only (refer sample tests for more clarity).

Matrix Rotation Visualization

It is guaranteed that the minimum of \$M\$ and \$N\$ will be even.

Input

First line contains three space separated integers, \$M\$, \$N\$ and \$R\$, where \$M\$ is the number of rows, \$N\$ is number of columns in matrix, and \$R\$ is the number of times the matrix has to be rotated. Then \$M\$ lines follow, where each line contains \$N\$ space separated positive integers. These \$M\$ lines represent the matrix.

Output

Print the rotated matrix.

Constraints

  • \$2 \le M, N \le 300\$
  • \$1 \le R \le 109\$
  • min(M, N) % 2 == 0
  • \$1 \le aij \le 108\$, where \$i \in [1..M]\$ & \$j \in [1..N]\$

Sample Input #00

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

Sample Output #00

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

Sample Input #01

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

Sample Output #01

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

Following is how I tried to solve the problem, but it runs just too slow:

from copy import deepcopy

aM, aN, R = map(int, input().split())

pivots = min(aM, aN)//2
refMat = []
baseMat = []

for i in range(aM):
    readInput = list(map(int, input().split()))
    baseMat.append(readInput)

refMat = deepcopy(baseMat)

for i in range(pivots):
    cLimit = (aN - 1) - i
    rLimit = (aM - 1) - i
    loopLength = (aM + aN - 2 - 4*i)*2 
    nbrOfRotations = R%loopLength

    for rotnIndex in range(nbrOfRotations):

        # Corner movements
        # Pivot
        refMat[i][i] = baseMat[i][i + 1]
        # Column End
        refMat[i][cLimit] = baseMat[i + 1][cLimit]
        # Row End
        refMat[rLimit][i] = baseMat[rLimit - 1][i]
        # Pivot diagonal
        refMat[rLimit][cLimit] = baseMat[rLimit][cLimit - 1]

        # Top movement
        for j in range(i+1, cLimit):
            refMat[i][j] = baseMat[i][j + 1]

        # Bottom movement
        for j in range(i+1, cLimit):
            refMat[rLimit][j] = baseMat[rLimit][j - 1]

        # Left movement
        for j in range(i+1, rLimit):
            refMat[j][i] = baseMat[j - 1][i]

        # Right movement
        for j in range(i+1, rLimit):
            refMat[j][cLimit] = baseMat[j + 1][cLimit]

        baseMat = deepcopy(refMat)

for i in refMat:
    for e in i:
        print(e, end = " ")
    print()

Note: No advanced libraries such as NumPy, SciPy etc. are allowed. However, I would love to know if they offer a better workaround.

share|improve this question
1  
It doesn’t seem that slow to me. Are there particular cases that are causing you problems? –  alexwlchan Jul 30 at 6:05
    
@alexwlchan, it does become slow when the inputs, for example, are of the dimension rows = 136, columns = 240 and rotations = 212131 –  Ben Sooraj M Jul 30 at 9:15
1  
I was wondering if there is any way to pre-compute the new position of any element, instead of shifting them around. Or some other better/efficient way to get this done. Thanks for your inputs. –  Ben Sooraj M Jul 30 at 9:18
    
There isn't really a need to actually move the elements, just update the starting one for each layer, and all the rest just follow behind it. As an example, if you are rotating 1 2 3 4 you can just start later on (at 2) and print each answer from there –  spyr03 Aug 3 at 19:54
1  
@BenSoorajM it may take a while to do the full code for me, I'll get back to you in a couple of days –  spyr03 Aug 3 at 21:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.