백준 17144 미세먼지 안녕!

wook2·2021년 7월 31일
0

알고리즘

목록 보기
44/117

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

bfs + 구현에 관한 문제이다.
update해주어야 하는 곳을 배열에 저장하고 한번에 update시켰다.
회전은 일일이 하나하나 다 구현했지만 시간이 많이 걸리는 느낌이 있었다.

r,c,t = list(map(int,input().split()))
board = []
for i in range(r):
    board.append(list(map(int,input().split())))
s = 0
dx = [-1,1,0,0]
dy = [0,0,-1,1]

for i in range(r):
    if board[i][0] == -1:
        s = i
        break

def diffusion():
    temp = []
    for i in range(r):
        for j in range(c):
            if board[i][j] >= 5:
                cnt = 0
                for k in range(4):
                    nx = i + dx[k]
                    ny = j + dy[k]
                    if 0 <= nx < r and 0 <= ny < c and board[nx][ny] != -1:
                        cnt += 1
                        temp.append((nx,ny,board[i][j] // 5))
                board[i][j] -= (board[i][j]//5) * cnt
    for t in temp:
        board[t[0]][t[1]] += t[2]

def clean():
    for i in range(s):
        board[s-i][0] = board[s-i-1][0]
    # print(board)
    for i in range(1,r-s-1):
        board[s+i][0] = board[s+i+1][0]
    # print(board)

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

    for i in range(s):
        board[i][c-1] = board[i+1][c-1]
    for i in range(1,r-s-1):
        board[r-i][c-1] = board[r-i-1][c-1]

    for i in range(1,c-1):
        board[s][c-i] = board[s][c-i-1]
        board[s+1][c-i] = board[s+1][c-i-1]

    board[s][1] = 0
    board[s+1][1] = 0
    board[s][0] = -1
    board[s+1][0] = -1

for i in range(t):
    diffusion()
    clean()

answer = 0
for row in board:
    answer += sum(row)

answer += 2

print(answer)
profile
꾸준히 공부하자

0개의 댓글