Untitled

mail@pastecode.io avatar
unknown
python
17 days ago
1.6 kB
1
Indexable
Never
def getFloor(i, j, lm, lv):
    return min(min(i, lm - i - 1), min(j, lv - j - 1))

def matrixRotation(matrix, r):
    # Write your code here
    temp = copy.deepcopy(matrix)
    lm = len(matrix)
    lv = len(matrix[0])
    for i, v1 in enumerate(matrix):
        for j, v2 in enumerate(v1):
            ni = i
            nj = j
            f = getFloor(i, j, lm, lv)
            lmf = lm - f
            lvf = lv - f
            # floor control
            # print("[{0}][{1}]: {2}".format(i, j, f))
            # print("[{0}][{1}]: {2}-{3}".format(i, j, lmf, lvf))
            nr = r % (((lm - (2 * f)) + (lv - (2 * f)) - 2) * 2)
            for x in range(nr):
                # directions control; 0-bottom, 1-right, 2-top, 3-left
                for y in range(4):
                    ti = ni
                    tj = nj
                    if y == 0 and ni + 1 < lmf and nj == f:
                        ti = ni + 1
                    elif y == 1 and nj + 1 < lvf and ni + 1 == lmf:
                        tj = nj + 1
                    elif y == 2 and ni > f:
                        ti = ni - 1
                    elif y == 3 and nj > f:
                        tj = nj - 1
                    if (ti != ni or tj != nj) and f == getFloor(ti, tj, lm, lv):
                        # print("[{0}][{1}]: [{2}][{3}] - {4}".format(i, j, ti, tj, v2))
                        ni = ti
                        nj = tj
                        break
            temp[ni][nj] = v2
    # print
    for x in temp:
        print(" ".join(str(v) for v in x))
Leave a Comment