[Programmers] - 행렬 테두리 회전하기 (Level 2)


2021 Dev-Matching:웹 백엔드 개발
LEVEL : 2
문제링크

rows = 3
columns = 3
queries = [[1, 1, 2, 2], [1, 2, 2, 3], [2, 1, 3, 2], [2, 2, 3, 3]]
def solution(rows, columns, queries):

    import numpy as np
    mat = np.zeros([rows,columns])
    n = 1
    for i in range(rows):
        for j in range(columns):
            mat[i][j] = n
            n+=1

    answer = []
    for a,b,c,d in queries:
        T,L,B,R = a-1, b-1, c-1, d-1

        tmpmat = np.copy(mat)
        tmp = []
        #Left
        for j in range(T,B):
            tmp.append(mat[j][L])
            mat[j][L] = tmpmat[j+1][L]

        #Right
        for j in range(T+1, B+1):
            tmp.append(mat[j][R])
            mat[j][R] = tmpmat[j-1][R]

        #Bottom
        for j in range(L, R):
            tmp.append(mat[B][j])
            mat[B][j] = tmpmat[B][j+1]

        #Top
        for j in range(L+1,R+1):
            tmp.append(mat[T][j])
            mat[T][j] = tmpmat[T][j-1]

        answer.append(min(tmp))

    return answer
solution(rows, columns, queries)
[1.0, 1.0, 5.0, 3.0]

Comments