1
+ import sys
2
+ from collections import deque
3
+ input = sys .stdin .readline
4
+
5
+ """
6
+ [문제 설명] - 단순 구현 문제
7
+ 봄: 하나의 칸마다 나이가 어린 나무부터 자신의 나이만큼 양분을 먹고, 나이가 1 증가함
8
+ 각 칸에 양분이 부족해 자신의 나이만큼 양분을 못 먹는 나무는 즉시 죽음
9
+ 여름: 봄에 죽은 나무가 양분으로 변함. 죽은 나무마다 나이를 2로 나눈 값이 양분으로 추가 (소수점 버림)
10
+ 가을: 나이가 5의 배수인 나무가 번식. 인접한 8개 칸에 나이가 1인 나무가 생김
11
+ 겨울: 로봇(S2D2)이 땅을 돌아다니면서 A[r][c]만큼 각 칸에 양분 추가
12
+
13
+ K년이 지난 후 상도의 땅에 살아있는 나무의 개수
14
+
15
+ [문제 풀이]
16
+ a: 로봇(S2D2)가 겨울에 주는 양분의 양
17
+ land: 땅의 현재 양분 상태
18
+ tree[i][j]: 해당 영역에 존재하는 나이와 개수를 튜플로 묶어서 덱에 저장
19
+ - 새로운 나무가 번식하기 때문에, 나이에 대한 오름차순을 유지하기 위해서는 앞에서의 삽입이 필요
20
+
21
+ """
22
+
23
+ # 봄을 거쳐 나이를 먹은 나무들에 의해 새롭게 태어나게 되는 나무의 수를 계산
24
+ def breeding (breeding_src ):
25
+ dr = [- 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 ]
26
+ dc = [- 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 ]
27
+
28
+ breeding_cnt = [[0 ]* n for _ in range (n )]
29
+
30
+ for r in range (n ):
31
+ for c in range (n ):
32
+ if breeding_src [r ][c ] == 0 :
33
+ continue
34
+ for i in range (8 ):
35
+ nr = r + dr [i ]
36
+ nc = c + dc [i ]
37
+ if 0 <= nr < n and 0 <= nc < n :
38
+ breeding_cnt [nr ][nc ] += breeding_src [r ][c ]
39
+
40
+ return breeding_cnt
41
+
42
+ # 봄과 여름을 묶어서 진행
43
+ def spring_summer ():
44
+ breeding_src = [[0 ]* n for _ in range (n )] # 나이가 5의 배수가 되어 가을에 번식을 하는 나무의 수를 각 영역에 저장
45
+
46
+ for i in range (n ):
47
+ for j in range (n ):
48
+ next_year = deque ()
49
+ dead = 0
50
+
51
+ while tree [i ][j ]:
52
+ age , cnt = tree [i ][j ].popleft ()
53
+ # 해당 나이의 모든 나무에게 양분을 줄 수 없는 경우
54
+ if land [i ][j ] < age * cnt :
55
+ dead = cnt - land [i ][j ] // age
56
+ cnt = land [i ][j ] // age # 살 수 있는 최대 수
57
+
58
+ if cnt > 0 :
59
+ land [i ][j ] -= age * cnt
60
+ next_year .append ((age + 1 , cnt ))
61
+
62
+ if (age + 1 ) % 5 == 0 :
63
+ breeding_src [i ][j ] += cnt
64
+
65
+ # 죽은 나무가 생기면 그 이후의 나무는 모두 죽게 된다.
66
+ if dead > 0 :
67
+ land [i ][j ] += (age // 2 ) * dead # 여름에 양분이 됨
68
+ break
69
+
70
+ # 여름 -> 죽은 나무들이 양분이 됨
71
+ while tree [i ][j ]:
72
+ age , dead = tree [i ][j ].popleft ()
73
+ land [i ][j ] += (age // 2 ) * dead
74
+
75
+ tree [i ][j ] = next_year
76
+
77
+ return breeding_src
78
+
79
+ def autumn_winter (breeding_src ):
80
+ # 봄에 나이를 먹은 나무들의 번식 결과
81
+ breeding_cnt = breeding (breeding_src )
82
+
83
+ for i in range (n ):
84
+ for j in range (n ):
85
+ # 가을 - 번식
86
+ if breeding_cnt [i ][j ]:
87
+ tree [i ][j ].appendleft ((1 , breeding_cnt [i ][j ]))
88
+ # 겨울 - 로봇에 의해 양분 추가
89
+ land [i ][j ] += winter_list [i ][j ]
90
+ return
91
+
92
+
93
+ # 입력
94
+ n , m , k = map (int , input ().split ())
95
+ winter_list = [list (map (int , input ().split ())) for _ in range (n )]
96
+
97
+ land = [[5 ]* n for _ in range (n )]
98
+ tree = [[deque () for _ in range (n )] for _ in range (n )] # -> 만약 여기서 [deque()] * n으로 하면 어떻게 될까요?
99
+
100
+ for _ in range (m ):
101
+ x , y , z = map (int , input ().split ())
102
+ tree [x - 1 ][y - 1 ].append ((z , 1 ))
103
+
104
+
105
+ # k년 동안 시뮬레이션
106
+ for _ in range (k ):
107
+ breeding_src = spring_summer ()
108
+ autumn_winter (breeding_src )
109
+
110
+ ans = 0
111
+
112
+ # 남아 있는 나무 수 카운트
113
+ for line in tree :
114
+ for area in line :
115
+ for _ , cnt in area :
116
+ ans += cnt
117
+
118
+ print (ans )
0 commit comments