Jae-Kyung Cho LLM Developers who was a Robotics engineer

Baekjoon-17822-원판 돌리기

from collections import deque
N,M,T = map(int,input().split())

board = [[]]
for _ in range(N):
    board.append(list(map(int,input().split())))
head = [0] * (N+1) # index of top element

for _ in range(T):
    x,d,k = map(int,input().split())

    # debug
    # print('-------------------')
    # for i in range(1,N+1):
    #     for j in range(M):
    #         print(board[i][(j+head[i])%M],end='')
    #     print()
    
    # 회전
    for i in range(0,N+1,x):
        head[i] = head[i]+k if d==1 else head[i]-k
        head[i] %= M

    # 인접 수 찾기
    visited = [[0] * M for _ in range(N+1)]
    visited[1][0] = 1
    sames = []
    is_removed = False
    total_sum, total_count = 0,0
    for i in range(1,N+1):
        for j in range(M):
            if board[i][(j+head[i])%M] == 0:
                continue
            else:
                total_sum += board[i][(j+head[i])%M]
                total_count += 1

                for ni,nj in [[i,(j+1)%M],[i,(j-1)%M],[i+1,j],[i-1,j]]:
                    if 1<=ni<=N:
                        if board[ni][(nj+head[ni])%M] == board[i][(j+head[i])%M]:
                            visited[i][j] = 2
                            visited[ni][nj] = 2
                            is_removed = True

    # debug
    # print('-------------------')
    # for i in range(1,N+1):
    #     for j in range(M):
    #         print(board[i][(j+head[i])%M],end='')
    #     print()

    if total_count == 0:
        print(0)
        exit()
    avg = total_sum / total_count
    for i in range(1,N+1):
        for j in range(M):
            if not is_removed:
                if board[i][j] != 0:
                    if board[i][j] > avg:
                        board[i][j] = board[i][j]-1
                    elif board[i][j] < avg:
                        board[i][j] = board[i][j]+1
            elif visited[i][j] == 2:
                board[i][(j+head[i])%M] = 0

    # debug
    # print('-------------------')
    # for i in range(1,N+1):
    #     for j in range(M):
    #         print(board[i][(j+head[i])%M],end='')
    #     print()

ans = 0
for i in range(1,N+1):
    for j in range(M):
        ans += board[i][j]
print(ans)
Comments