[Python] 백준17144 미세먼지 안녕!
문제
예제 입력
코드
import sys
R, C, T = map(int, input().split())
arr = []
for _ in range(R):
arr.append(list(map(int, sys.stdin.readline().split())))
air = []
for i in range(R):
for j in range(C):
if arr[i][j] == -1:
air.append([i, j])
def sol():
dust = []
for i in range(R):
for j in range(C):
if arr[i][j] != 0 and arr[i][j] != -1:
dust.append([i, j])
tmp = [[0]*C for _ in range(R)]
for y, x in dust:
cnt = 0
for dy, dx in [[-1, 0],[1, 0],[0, -1],[0, 1]]:
newy = y + dy
newx = x + dx
if 0 <= newy < R and 0 <= newx < C and arr[newy][newx] != -1:
tmp[newy][newx] += arr[y][x] // 5
cnt += 1
arr[y][x] -= (arr[y][x] // 5) * cnt
for i in range(R):
for j in range(C):
if arr[i][j] != -1:
arr[i][j] += tmp[i][j]
for _ in range(T):
sol()
# 아래
for t in range(1, air[0][0]):
arr[air[0][0]-t][0] = arr[air[0][0]-t-1][0]
# 왼쪽
for t in range(0, C-1):
arr[0][t] = arr[0][t+1]
# 위
for t in range(0, air[0][0]):
arr[t][-1] = arr[t+1][-1]
# 오른쪽
for t in range(C-2, 0, -1):
arr[air[0][0]][t+1] = arr[air[0][0]][t]
arr[air[0][0]][1] = 0
# 위
for t in range(air[1][0]+2, R):
arr[t-1][0] = arr[t][0]
# 왼쪽
for t in range(1, C):
arr[-1][t-1] = arr[-1][t]
# 아래
for t in range(R-2, air[1][0]-1, -1):
arr[t+1][-1] = arr[t][-1]
# 오른쪽
for t in range(C-2, 0, -1):
arr[air[1][0]][t+1] = arr[air[1][0]][t]
arr[air[1][0]][1] = 0
print(sum(map(sum, arr))+2)
설명
파이썬을 통해서 사용자로부터 입력받아 시뮬레이션(구현) 알고리즘을 사용하여 미세먼지 안녕!를 구현했습니다.