[알고리즘/백준] 16926번 : 배열 돌리기 1(python)

유현민·2022년 5월 3일
0

알고리즘

목록 보기
163/253

큐를 이용해서 풀었다.
기준을 잡아서 기준을 따라 겉을 한 바퀴 돌면 큐에 다 들어가게 되고 그걸 rotate로 밀어주면 된다.

0,0 -> 1,1 -> 2,2... 처음 시작은 항상 이렇게 해주었다.
만약 -2를 계속 빼서 0이 나오면 마지막이기 때문에 그만했다.

import sys
from collections import deque

input = sys.stdin.readline


def answer():
    for i in range(x, x + w):
        q.append(a[y][i])

    for i in range(y + 1, y + h):
        q.append(a[i][x + w - 1])

    for i in range(x + w - 2, x, -1):
        q.append(a[y + h - 1][i])

    for i in range(y + h - 1, y, -1):
        q.append(a[i][x])

    q.rotate(-R)

    for i in range(x, x + w):
        a[y][i] = q.popleft()

    for i in range(y + 1, y + h):
        a[i][x + w - 1] = q.popleft()

    for i in range(x + w - 2, x, -1):
        a[y + h - 1][i] = q.popleft()

    for i in range(y + h - 1, y, -1):
        a[i][x] = q.popleft()


N, M, R = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(N)]
q = deque()
h = N
w = M
x, y = 0, 0
while True:
    if h == 0 or w == 0:
        break
    answer()
    y += 1
    x += 1
    h -= 2
    w -= 2

for i in a:
    print(*i)
profile
smilegate megaport infra

0개의 댓글