Skip to content

Commit 8699457

Browse files
committed
[BOJ] 미세먼지 안녕! / 골드 4 / 80분
https://www.acmicpc.net/problem/17144
1 parent fc45aa7 commit 8699457

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import sys
2+
3+
# 입력 받기
4+
R, C, T = map(int, sys.stdin.readline().split())
5+
pan = [list(map(int, sys.stdin.readline().split())) for _ in range(R)]
6+
7+
# 방향 설정 (상, 우, 하, 좌)
8+
dx = [-1, 0, 1, 0]
9+
dy = [0, 1, 0, -1]
10+
11+
# 공기 청정기 위치 찾기
12+
cleaner = []
13+
for i in range(R):
14+
if pan[i][0] == -1:
15+
cleaner.append(i)
16+
17+
# 먼지 확산 함수
18+
def spread():
19+
temp = [[0] * C for _ in range(R)]
20+
for x in range(R):
21+
for y in range(C):
22+
if pan[x][y] > 0:
23+
value = pan[x][y] // 5
24+
count = 0
25+
for d in range(4):
26+
nx, ny = x + dx[d], y + dy[d]
27+
if 0 <= nx < R and 0 <= ny < C and pan[nx][ny] != -1:
28+
temp[nx][ny] += value
29+
count += 1
30+
pan[x][y] -= value * count
31+
for x in range(R):
32+
for y in range(C):
33+
pan[x][y] += temp[x][y]
34+
35+
# 공기 청정기 작동 함수
36+
def clean():
37+
# 위쪽 청정기
38+
top = cleaner[0]
39+
for i in range(top - 1, 0, -1):
40+
pan[i][0] = pan[i - 1][0]
41+
for i in range(C - 1):
42+
pan[0][i] = pan[0][i + 1]
43+
for i in range(top):
44+
pan[i][C - 1] = pan[i + 1][C - 1]
45+
for i in range(C - 1, 1, -1):
46+
pan[top][i] = pan[top][i - 1]
47+
pan[top][1] = 0
48+
49+
# 아래쪽 청정기
50+
bottom = cleaner[1]
51+
for i in range(bottom + 1, R - 1):
52+
pan[i][0] = pan[i + 1][0]
53+
for i in range(C - 1):
54+
pan[R - 1][i] = pan[R - 1][i + 1]
55+
for i in range(R - 1, bottom, -1):
56+
pan[i][C - 1] = pan[i - 1][C - 1]
57+
for i in range(C - 1, 1, -1):
58+
pan[bottom][i] = pan[bottom][i - 1]
59+
pan[bottom][1] = 0
60+
61+
# 시뮬레이션 실행
62+
for _ in range(T):
63+
spread()
64+
clean()
65+
66+
# 남은 미세먼지 계산
67+
result = sum(sum(row) for row in pan) + 2 # 공기청정기 위치(-1)를 더하지 않음
68+
print(result)

0 commit comments

Comments
 (0)