-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaek17144.py
200 lines (190 loc) · 5.96 KB
/
baek17144.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from sys import stdin
from collections import deque
R, C, T = map(int, stdin.readline().split(" "))
room = []
for _ in range(R):
room.append(list(map(int, stdin.readline().split(" "))))
# 미세먼지 탐색
AP = []
AC = []
for i in range(R):
for j in range(C):
if room[i][j] == -1:
AC.append((i,j))
elif room[i][j] != 0:
AP.append((i, j))
room[i][j] = [room[i][j],0]# (원래 있던 양, 확산으로 들어옴)
#AC.sort(key=lambda x:x[0])# 에어컨 세우기
# BFS & Simulation
for _ in range(T):
# 미세먼지 확산
diffs = []
for k in range(len(AP)):
i, j = AP[k]
Aij = room[i][j][0]
diff = Aij//5# 확산될 량
count = 0
if not i == 0:
if not room[i - 1][j][0] == -1:
room[i - 1][j][1] += diff
diffs.append((i - 1, j))
count += 1
if not i == R - 1:
if not room[i + 1][j][0] == -1:
room[i + 1][j][1] += diff
diffs.append((i + 1, j))
count += 1
if not j == 0:
if not room[i][j - 1][0] == -1:
room[i][j - 1][1] += diff
diffs.append((i, j - 1))
count += 1
if not j == C - 1:
if not room[i][j + 1][0] == -1:
room[i][j + 1][1] += diff
diffs.append((i, j + 1))
count += 1
room[i][j][0] -= diff*count
#잔류 량과 확산온 량 합산
for i,j in diffs:
if room[i][j][0] == 0:
AP.append((i,j))
room[i][j][0] += room[i][j][1]
room[i][j][1] = 0
APSet = set(AP)
# 과제: 공기 청정기로 인한 AP 리스트의 변화 반영하기!!!
# 공기 청정기 효과 계산
# 윗부분
i,j = AC[0]
room[i][j][1] += room[i-1][j][0]
room[i - 1][j][0] = 0
if room[i-1][j][0] > 0:
APSet.add((i,j))
if (i-1,j)in APSet:
APSet.remove((i-1,j))
topNow = i-2,j
topDirect = "D"
while topNow != AC[0]:
if topDirect == "D":
nowi, nowj = topNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi+1, nowj))
if topNow in APSet:
APSet.remove(topNow)
room[nowi+1][nowj][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowi == 0:
topDirect = "L"
nowj += 1
else:
nowi -= 1
elif topDirect == "L":
nowi, nowj = topNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi, nowj-1))
if topNow in APSet:
APSet.remove(topNow)
room[nowi][nowj - 1][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowj == C-1:
topDirect = "U"
nowi += 1
else:
nowj += 1
elif topDirect == "U":
nowi, nowj = topNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi-1, nowj))
if topNow in APSet:
APSet.remove(topNow)
room[nowi - 1][nowj][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowi == AC[0][0]:
topDirect = "R"
nowj -= 1
else:
nowi += 1
elif topDirect == "R":
nowi, nowj = topNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi, nowj+1))
if topNow in APSet:
APSet.remove(topNow)
room[nowi][nowj + 1][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowj == AC[0][1]+1:
break
else:
nowj -= 1
topNow = (nowi, nowj)
# 아랫부분
i, j = AC[1]
room[i][j][1] += room[i+1][j][0]
room[i + 1][j][0] = 0
if room[i+1][j][0] > 0:
APSet.add((i,j))
if (i+1,j) in APSet:
APSet.remove((i+1,j))
bottomNow = i + 2, j
bottomDirect = "U"
while bottomNow != AC[1]:
if bottomDirect == "U":
nowi, nowj = bottomNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi-1, nowj))
if bottomNow in APSet:
APSet.remove(bottomNow)
room[nowi - 1][nowj][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowi == R - 1:
bottomDirect = "L"
nowj += 1
else:
nowi += 1
elif bottomDirect == "L":
nowi, nowj = bottomNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi, nowj-1))
if bottomNow in APSet:
APSet.remove(bottomNow)
room[nowi][nowj - 1][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowj == C - 1:
bottomDirect = "D"
nowi -= 1
else:
nowj += 1
elif bottomDirect == "D":
nowi, nowj = bottomNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi+1, nowj))
if bottomNow in APSet:
APSet.remove(bottomNow)
room[nowi + 1][nowj][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowi == AC[1][0]:
bottomDirect = "R"
nowj -= 1
else:
nowi -= 1
elif bottomDirect == "R":
nowi, nowj = bottomNow
if room[nowi][nowj][0] > 0:
APSet.add((nowi, nowj+1))
if bottomNow in APSet:
APSet.remove(bottomNow)
room[nowi][nowj + 1][0] = room[nowi][nowj][0]
room[nowi][nowj][0] = 0
if nowj == AC[1][1]+1:
break
else:
nowj -= 1
bottomNow = (nowi, nowj)
AP = list(APSet)
sumAP = 0
for i in range(R):
for j in range(C):
if room[i][j][0] > 0:
sumAP += room[i][j][0]
# result
print(sumAP)