흙금이네 블로그

[BOJ] 17822 - 원판 돌리기 (Python) 본문

알고리즘

[BOJ] 17822 - 원판 돌리기 (Python)

흙금 2023. 3. 23. 13:41

 

 

아이디어

 

회전 방법에 따라 원판을 회전한 후 원판에 적힌 수의 합을 구한다.

 

 

풀이

 

import sys

input = sys.stdin.readline

def solution():
    N, M, T = map(int, input().split())
    board = [list(map(int, input().split())) for _ in range(N)]
    cnt = N*M
    total = 0
    for i in range(N):
        total += sum(board[i])
    for t in range(T):
        x, d, k = map(int, input().split())
        if d == 0:
            k = -k
        for i in range(x-1, N, x):
            board[i] = board[i][k:]+board[i][:k]
        temp = set()
        for i in range(N):
            for j in range(M):
                if board[i][j]:
                    if board[i][j] == board[i][j-1]:
                        temp.add((i, j))
                        temp.add((i, j-1))
        for i in range(N-1):
            for j in range(M):
                if board[i][j]:
                    if board[i][j] == board[i+1][j]:
                        temp.add((i, j))
                        temp.add((i+1, j))
        if temp:
            for i, j in temp:
                if board[i][j]:
                    total -= board[i][j]
                    cnt -= 1
                    board[i][j] = 0
        else:
            if cnt:
                avg = total/cnt
                for i in range(N):
                    for j in range(M):
                        if board[i][j]:
                            if board[i][j] > avg:
                                board[i][j] -= 1
                                total -= 1
                            elif board[i][j] < avg:
                                board[i][j] += 1
                                total += 1
    print(total)

solution()

 

Comments