목차

행렬 테두리 회전하기

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호77485
문제명행렬 테두리 회전하기
레벨Level 2
분류

구현

시간복잡도O(n(r+c))
인풋사이즈n<=10000, r<=100, c<=100
사용한 언어Python
해결날짜2021/07/07
출처

ps:problems:programmers:2021_dev-matching_-_웹_백엔드_개발자_상반기

풀이

코드

"""Solution code for "Programmers 77485. 행렬 테두리 회전하기".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/77485
- Solution link: http://www.teferi.net/ps/problems/programmers/77485
"""

import itertools


def border_positions(x1, y1, x2, y2):
    return itertools.chain(((x - 1, y1 - 1) for x in range(x1, x2 + 1)),
                           ((x2 - 1, y - 1) for y in range(y1, y2 + 1)),
                           ((x - 1, y2 - 1) for x in range(x2, x1 - 1, -1)),
                           ((x1 - 1, y - 1) for y in range(y2, y1 - 1, -1)))


def solution(rows, columns, queries):
    answers = []
    i = 0
    grid = [[(i := i + 1) for _ in range(columns)] for _ in range(rows)]
    for query in queries:
        positions = border_positions(*query)
        prev_positions = border_positions(*query)
        x, y = next(prev_positions)
        tmp = grid[x][y]
        for (x, y), (prev_x, prev_y) in zip(positions, prev_positions):
            grid[x][y] = grid[prev_x][prev_y]
        grid[x][y] = tmp
        answers.append(min(grid[x][y] for x, y in border_positions(*query)))
    return answers