코딩

[BOJ, 백준] 16935 배열 돌리기 3 Python

척척석사아님 2024. 7. 23. 15:52
728x90

 

문제는 다음과 같습니다.

 

 

https://www.acmicpc.net/problem/16935

 

Typical한 구현 문제였고, 케이스를 잘 나누어주면서 풀어주면 되겠습니다.

이러한 문제들의 경우, 몇가지 특이한 케이스가 어디로 이동하게 되는지 체크하면, 규칙성이 눈에 보이기 쉬운 문제 같습니다.

해답 코드는 아래와 같습니다.

import sys
input = sys.stdin.readline

N, M, R = map(int,input().split())
mat = []
for _ in range (0,N):
    a = list(map(int,input().split()))
    mat.append(a)
def oper(k,mat):
    column = len(mat)
    row = len(mat[0])
    if k == 1: #상하반전
        new_mat = []
        for i in range (column-1,-1,-1):
            new_mat.append(mat[i])

    elif k == 2: #좌우반전
        new_mat = []
        for i in range (0,column):
            new_col = []
            for j in range (row-1,-1,-1):
                new_col.append(mat[i][j])
            new_mat.append(new_col)

    elif k == 3: #오른쪽으로 90도 회전
        new_mat = [[0 for _ in range (0,column)] for _ in range (0,row)]
        for i in range (0,column):
            for j in range (0,row):
                new_mat[j][column-1-i] = mat[i][j]

    elif k == 4: #왼쪽으로 90도 회전
        new_mat = [[0 for _ in range(0, column)] for _ in range(0, row)]
        for i in range (0,column):
            for j in range (0,row):
                new_mat[row-1-j][i] = mat[i][j]

    elif k == 5 or k == 6: #4개로 나눈 뒤에 시계방향 회전
        n = column // 2
        m = row // 2
        new_mat = [[0 for _ in range (0,row)] for _ in range (0,column)]
        block_1, block_2, block_3, block_4 = [],[],[],[]
        for i in range (0,n):
            t = []
            for j in range (0,m):
                t.append(mat[i][j])
            block_1.append(t)

        for i in range (0,n):
            t = []
            for j in range (m,row):
                t.append(mat[i][j])
            block_2.append(t)

        for i in range (n,column):
            t = []
            for j in range (0,m):
                t.append(mat[i][j])
            block_4.append(t)

        for i in range (n,column):
            t = []
            for j in range (m,row):
                t.append(mat[i][j])
            block_3.append(t)

        #분할
        if k == 5:
            for i in range (0,column):
                for j in range (0,row):
                    if 0 <= i < n and 0 <= j < m:
                        new_mat[i][j] = block_4[i][j]
                    elif 0 <= i < n and m <= j < row:
                        new_mat[i][j] = block_1[i][j-m]
                    elif n <= i < column and m <= j < row:
                        new_mat[i][j] = block_2[i-n][j-m]
                    elif n <= i < column and 0 <= j < m:
                        new_mat[i][j] = block_3[i-n][j]

        elif k == 6:
            for i in range (0,column):
                for j in range (0,row):
                    if 0 <= i < n and 0 <= j < m:
                        new_mat[i][j] = block_2[i][j]
                    elif 0 <= i < n and m <= j < row:
                        new_mat[i][j] = block_3[i][j-m]
                    elif n <= i < column and m <= j < row:
                        new_mat[i][j] = block_4[i-n][j-m]
                    elif n <= i < column and 0 <= j < m:
                        new_mat[i][j] = block_1[i-n][j]
    return new_mat

cmd = list(map(int,input().split()))
for i in range (0,R):
    mat = oper(cmd[i],mat)

for i in range (0,len(mat)):
    print(*mat[i])