본문 바로가기

컴퓨터/코테

백준 17114 - 미세먼지 확산

빡구현 문제였다. 카카오 기출 카드짝 맞추기도 그렇고 이번문제도 그렇고 이것저것 많이 풀어보다보니 확실히 구현문제 실력이 성장한 것 같다.

def spread(arr, height, width, cleaner) :
    spreadList = []
    direction = {
        0:[0,-1],
        1:[0,1],
        2:[-1,0],
        3:[1,0]
    }
    newArr = [[ 0 for _ in range(width)] for i in range(height)]
    newArr[cleaner[0]][0] = -1
    newArr[cleaner[1]][0] = -1

    for y in range(height):
        for x in range(width):
            if arr[y][x] == -1 :
                continue
            if arr[y][x] != 0:
                dir = [1,1,1,1] # 상 하 좌 우
                if x == 0 or arr[y][x-1] == -1:
                    dir[2] = 0
                elif x == width-1:
                    dir[3] = 0
                if y == 0 or arr[y-1][x] == -1:
                    dir[0] = 0
                elif y == height-1 or arr[y+1][x] == -1:
                    dir[1] = 0
                spreadCount = dir.count(1)
                center = arr[y][x] - (arr[y][x] // 5) * spreadCount
                spreadAmount = arr[y][x] // 5
                newArr[y][x] += center
                for idx , i in enumerate(dir) :
                    if i :
                        moveX, moveY = direction[idx]
                        newArr[y + moveY][x + moveX] += spreadAmount
    return newArr
def moveDust(arr, upY, downY) :
    nextValue = 0
    for i in range(1,len(arr[0])):
        nextValue, arr[upY][i] = arr[upY][i], nextValue
    for i in range(upY-1, -1, -1):
        tmp = nextValue
        nextValue = arr[i][-1]
        arr[i][-1] = tmp
    for i in range(len(arr[0])-2,-1,-1):
        tmp = nextValue
        nextValue = arr[0][i]
        arr[0][i] = tmp
    for i in range(1, upY) :
        tmp = nextValue
        nextValue = arr[i][0]
        arr[i][0] = tmp

    nextValue = 0
    for i in range(1, len(arr[0])):
        tmp = nextValue
        nextValue = arr[downY][i]
        arr[downY][i] = tmp
    for i in range(downY+1, len(arr)):
        tmp = nextValue
        nextValue = arr[i][-1]
        arr[i][-1] = tmp
    for i in range(len(arr[0]) - 2, -1, -1):
        tmp = nextValue
        nextValue = arr[-1][i]
        arr[-1][i] = tmp
    for i in range(len(arr)-2, downY,-1):
        tmp = nextValue
        nextValue = arr[i][0]
        arr[i][0] = tmp


r, c, t = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(r)]
cleaner = []
for i in range(r):
    if arr[i][0] == -1:
        cleaner.append(i)
        cleaner.append(i+1)
        break

for i in range(t) :
    arr = spread(arr, r, c, cleaner)
    moveDust(arr, cleaner[0], cleaner[1])

sum = 2
for i in range(r):
    for j in range(c):
        sum += arr[i][j]
print(sum)

'컴퓨터 > 코테' 카테고리의 다른 글

nQueen - 백트래킹  (0) 2021.09.01
백준 2042 세그먼트 트리  (0) 2021.09.01
백준 11657 타임머신  (0) 2021.08.30
카카오 기출 - 카드 뒤집기  (0) 2021.08.27
카카오 기출 - 표편집  (0) 2021.08.27