Skip to content

Commit 0ca7f27

Browse files
committed
[BOJ] 17144 미세먼지 안녕! (G4)
1 parent bdcb3c0 commit 0ca7f27

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

최어진/8주차/260218.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# 백준 17144번: 미세먼지 안녕! (3회차)
2+
3+
import sys
4+
5+
input = sys.stdin.readline
6+
7+
# R, C <= 50
8+
# T <= 10^3
9+
R, C, T = map(int, input().rstrip().split())
10+
maps = [list(map(int, input().rstrip().split())) for _ in range(R)]
11+
12+
EMPTY = 0
13+
CLEANER = -1
14+
15+
cleaner_r_top, cleaner_r_bottom = -1, -1
16+
for r in range(R):
17+
if maps[r][0] == CLEANER:
18+
cleaner_r_top, cleaner_r_bottom = r, r + 1
19+
break
20+
21+
# 1초 동안 일어나는 일
22+
# 1. 확산: 모든 칸마다 4개 방향 -> 4 * 50 * 50 <= 10^4
23+
# 2. 순환: 최대 모든 칸 < 10^4
24+
# 주어진 시간은 T초 < 10^3
25+
# 예상: 10^7~10^8 이내
26+
27+
moves = [
28+
[-1, 0],
29+
[1, 0],
30+
[0, -1],
31+
[0, 1]
32+
]
33+
34+
def is_empty(r, c):
35+
return maps[r][c] == EMPTY
36+
37+
def is_cleaner(r, c):
38+
return (cleaner_r_top <= r <= cleaner_r_bottom) and c == 0
39+
40+
def is_in_board(r, c):
41+
return 0 <= r < R and 0 <= c < C
42+
43+
def print_board():
44+
for row in maps:
45+
print(row)
46+
print()
47+
48+
def diffusion():
49+
diffused_maps = [[0 for _ in range(C)] for _ in range(R)]
50+
51+
for r in range(R):
52+
for c in range(C):
53+
if is_cleaner(r, c): continue
54+
if is_empty(r, c): continue
55+
56+
diffused_maps[r][c] += maps[r][c]
57+
unit_diffusion = maps[r][c] // 5
58+
for dr, dc in moves:
59+
if is_in_board(r + dr, c + dc) and not is_cleaner(r + dr, c + dc):
60+
diffused_maps[r + dr][c + dc] += unit_diffusion
61+
diffused_maps[r][c] -= unit_diffusion
62+
63+
diffused_maps[cleaner_r_top][0], diffused_maps[cleaner_r_bottom][0] = -1, -1
64+
return diffused_maps
65+
66+
def circulation():
67+
# upper circulation
68+
for r in range(cleaner_r_top - 1, 0, -1):
69+
maps[r][0] = maps[r - 1][0]
70+
for c in range(C - 1):
71+
maps[0][c] = maps[0][c + 1]
72+
for r in range(cleaner_r_top):
73+
maps[r][C - 1] = maps[r + 1][C - 1]
74+
for c in range(C - 1, 1, -1):
75+
maps[cleaner_r_top][c] = maps[cleaner_r_top][c - 1]
76+
maps[cleaner_r_top][1] = 0
77+
78+
# lower circulation
79+
for r in range(cleaner_r_bottom + 1, R - 1):
80+
maps[r][0] = maps[r + 1][0]
81+
for c in range(C - 1):
82+
maps[R - 1][c] = maps[R - 1][c + 1]
83+
for r in range(R - 1, cleaner_r_bottom, -1):
84+
maps[r][C - 1] = maps[r - 1][C - 1]
85+
for c in range(C - 1, 1, -1):
86+
maps[cleaner_r_bottom][c] = maps[cleaner_r_bottom][c - 1]
87+
maps[cleaner_r_bottom][1] = 0
88+
89+
def count_dusts():
90+
cnt = 0
91+
for r in range(R):
92+
for c in range(C):
93+
if not is_cleaner(r, c):
94+
cnt += maps[r][c]
95+
return cnt
96+
97+
for _ in range(T):
98+
maps = diffusion()
99+
# print_board()
100+
circulation()
101+
# print_board()
102+
# print('------------')
103+
104+
print(count_dusts())

0 commit comments

Comments
 (0)