행렬 테두리 회전하기

참치돌고래·2021년 8월 30일
0

알고리즘

목록 보기
16/36
post-custom-banner

https://programmers.co.kr/learn/courses/30/lessons/77485


from collections import deque

def solution(rows, columns, queries):
    answer = []
    queue = deque()
    cnt=1
    map = [[(i+1)+columns*j for i in range(columns)] for j in range(rows)]
    
    for query in queries:
        x1,y1,x2,y2 = query
        # 큐에 사각형 넣기
        for i in range(y1-1,y2):
            queue.append(map[x1-1][i])
        for i in range(x1,x2):
            queue.append(map[i][y2-1])
        for i in range(y2-2,y1-2,-1):
            queue.append(map[x2-1][i])
        for i in range(x2-2,x1-1,-1):
            queue.append(map[i][y1-1])
        
        answer.append(min(queue))
        
        # 큐에서 사각형 돌면서 넣기
        while queue:
            for i in range(y1,y2):
                map[x1-1][i]=queue.popleft()
            for i in range(x1,x2):
                map[i][y2-1]=queue.popleft()
            for i in range(y2-2,y1-2,-1):
                map[x2-1][i]=queue.popleft()
            for i in range(x2-2,x1-2,-1):
                map[i][y1-1]=queue.popleft()
            
    
        
        
        
            
    return answer
profile
안녕하세요
post-custom-banner

0개의 댓글