Point πŸ’‘


dx = [-1,0,1,0] # 상 우 ν•˜ 쒌
dy = [0,1,0,-1]  
def clean(a,b):
    # μœ„μ— νšŒμ „
    x, y = a, 1
    index = 1 # μš°λΆ€ν„° μ‹œμž‘ 우 상 쒌 ν•˜
    temp = 0 # κ³΅κΈ°μ²­μ •κΈ°μ—μ„œ λ‚˜μ˜€λŠ” λ°”λžŒ
    while True:
        nx = x+dx[index]
        ny = y+dy[index]
        if nx==R or ny == C or nx==-1 or ny==-1: # 벽에 λ‹Ώμ•˜μ„ λ•Œ
            index = (index-1)%4
            continue
        if x==a and y==0: # κ³΅κΈ°μ²­μ •κΈ°λ‘œ λ‹€μ‹œ λŒμ•„μ˜΄
            break
        pan[x][y], temp = temp, pan[x][y] # swap
        x,y = nx, ny
    # μ•„λž˜ νšŒμ „
    x, y = b, 1
    index = 1 # μš°λΆ€ν„° μ‹œμž‘ 우 ν•˜ 쒌 상
    temp = 0 # κ³΅κΈ°μ²­μ •κΈ°μ—μ„œ λ‚˜μ˜€λŠ” λ°”λžŒ
    while True:
        nx = x+dx[index]
        ny = y+dy[index]
        if nx==R or ny == C or nx==-1 or ny==-1: # 벽에 λ‹Ώμ•˜μ„ λ•Œ
            index = (index+1)%4
            continue
        if x==b and y==0: # κ³΅κΈ°μ²­μ •κΈ°λ‘œ λ‹€μ‹œ λŒμ•„μ˜΄
            break
        pan[x][y], temp = temp, pan[x][y] # swap
        x,y = nx, ny

My Solution


# ν™•μ‚°
def spread(dust):
    for i in range(len(dust)):
        amount = dust[i][2] // 5
        cnt = 0
        for j in range(4):
            nx = dust[i][0] + dx[j]
            ny = dust[i][1] + dy[j]

            if nx < 0 or nx >= r or ny < 0 or ny >= c or room[nx][ny] == -1:
                continue

            room[nx][ny] += amount
            cnt += 1

        room[dust[i][0]][dust[i][1]] -= amount * cnt

# 곡기청정기 μž‘λ™ : λ’€μ—μ„œλΆ€ν„°?
def work(x, mode):

    if mode == 0:

        for i in range(x-1, 0, -1):
            room[i][0] = room[i-1][0]

        for i in range(c-1):
            room[0][i] = room[0][i+1]

        for i in range(x):
            room[i][c-1] = room[i+1][c-1]

        for i in range(c-1, 0, -1):
            if room[x][i-1] == -1:
                room[x][i] = 0
                break
            room[x][i] = room[x][i-1]

    else:

        for i in range(x+1, r-1):
            room[i][0] = room[i+1][0]

        for i in range(c-1):
            room[r-1][i] = room[r-1][i+1]

        for i in range(r-1, x, -1):
            room[i][c-1] = room[i-1][c-1]

        for i in range(c-1, 0, -1):
            if room[x][i-1] == -1:
                room[x][i] = 0
                break
            room[x][i] = room[x][i - 1]

import sys
input = sys.stdin.readline
r, c, t = map(int, input().split())
room = [list(map(int, input().split())) for _ in range(r)]

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

for i in range(t):
    dust = []
    for j in range(r):
        for k in range(c):
            if room[j][k] != 0 and room[j][k] != -1:
                dust.append((j, k, room[j][k]))

    spread(dust)

    index = 0
    for j in range(r):
        if room[j][0] == -1:
            work(j, index)
            index += 1

answer = 0
for i in range(r):
    answer += sum(room[i])

# -1 2개 빼주기
print(answer+2)