Post

๐Ÿข ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!

1. ๋ฌธ์ œ ๋งํฌ

17144๋ฒˆ: ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!


2. ์ฝ”๋“œ

Python3 31256KB 4372ms

PyPy3 165244KB 728ms

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
"""
[17144] ๋ฏธ์„ธ๋จผ์ง€ ์•ˆ๋…•!

๐Ÿ’› ๋ฌธ์ œ
๋ฏธ์„ธ๋จผ์ง€๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ์„ฑ๋Šฅ์„ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด ๊ตฌ์‚ฌ๊ณผ๋Š” ์ง‘์„ ํฌ๊ธฐ๊ฐ€ Rร—C์ธ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ๊ณ , 1ร—1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆด๋‹ค.
๊ตฌ์‚ฌ๊ณผ๋Š” ๋›ฐ์–ด๋‚œ ์ฝ”๋”ฉ ์‹ค๋ ฅ์„ ์ด์šฉํ•ด ๊ฐ ์นธ (r, c)์— ์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ชจ๋‹ˆํ„ฐ๋งํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ–ˆ๋‹ค.
(r, c)๋Š” rํ–‰ c์—ด์„ ์˜๋ฏธํ•œ๋‹ค

๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋Š” ํ•ญ์ƒ 1๋ฒˆ ์—ด์— ์„ค์น˜๋˜์–ด ์žˆ๊ณ , ํฌ๊ธฐ๋Š” ๋‘ ํ–‰์„ ์ฐจ์ง€ํ•œ๋‹ค.
๊ณต๊ธฐ์ฒญ์ •๊ธฐ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์€ ์นธ์—๋Š” ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ์žˆ๊ณ , (r, c)์— ์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์€ Ar,c์ด๋‹ค.

1์ดˆ ๋™์•ˆ ์•„๋ž˜ ์ ํžŒ ์ผ์ด ์ˆœ์„œ๋Œ€๋กœ ์ผ์–ด๋‚œ๋‹ค.
1. ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ํ™•์‚ฐ๋œ๋‹ค. ํ™•์‚ฐ์€ ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ์žˆ๋Š” ๋ชจ๋“  ์นธ์—์„œ ๋™์‹œ์— ์ผ์–ด๋‚œ๋‹ค.
    (r, c)์— ์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€๋Š” ์ธ์ ‘ํ•œ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ํ™•์‚ฐ๋œ๋‹ค.
    ์ธ์ ‘ํ•œ ๋ฐฉํ–ฅ์— ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๊ฐ€ ์žˆ๊ฑฐ๋‚˜, ์นธ์ด ์—†์œผ๋ฉด ๊ทธ ๋ฐฉํ–ฅ์œผ๋กœ๋Š” ํ™•์‚ฐ์ด ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.
    ํ™•์‚ฐ๋˜๋Š” ์–‘์€ Ar,c/5์ด๊ณ  ์†Œ์ˆ˜์ ์€ ๋ฒ„๋ฆฐ๋‹ค.
    (r, c)์— ๋‚จ์€ ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์€ Ar,c - (Ar,c/5)ร—(ํ™•์‚ฐ๋œ ๋ฐฉํ–ฅ์˜ ๊ฐœ์ˆ˜) ์ด๋‹ค.
2. ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๊ฐ€ ์ž‘๋™ํ•œ๋‹ค.
    ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์—์„œ๋Š” ๋ฐ”๋žŒ์ด ๋‚˜์˜จ๋‹ค.
    ์œ„์ชฝ ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ๋ฐ”๋žŒ์€ ๋ฐ˜์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ์ˆœํ™˜ํ•˜๊ณ , ์•„๋ž˜์ชฝ ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ๋ฐ”๋žŒ์€ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ์ˆœํ™˜ํ•œ๋‹ค.
    ๋ฐ”๋žŒ์ด ๋ถˆ๋ฉด ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ๋ฐ”๋žŒ์˜ ๋ฐฉํ–ฅ๋Œ€๋กœ ๋ชจ๋‘ ํ•œ ์นธ์”ฉ ์ด๋™ํ•œ๋‹ค.
    ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์—์„œ ๋ถ€๋Š” ๋ฐ”๋žŒ์€ ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ์—†๋Š” ๋ฐ”๋žŒ์ด๊ณ , ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๋กœ ๋“ค์–ด๊ฐ„ ๋ฏธ์„ธ๋จผ์ง€๋Š” ๋ชจ๋‘ ์ •ํ™”๋œ๋‹ค.

๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ๋ฐ”๋žŒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉํ–ฅ์œผ๋กœ ์ˆœํ™˜ํ•œ๋‹ค.
๋ฐฉ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, T์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„ ๊ตฌ์‚ฌ๊ณผ์˜ ๋ฐฉ์— ๋‚จ์•„์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์„ ๊ตฌํ•ด๋ณด์ž.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ์งธ ์ค„์— R, C, T (6 โ‰ค R, C โ‰ค 50, 1 โ‰ค T โ‰ค 1,000) ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
๋‘˜์งธ ์ค„๋ถ€ํ„ฐ R๊ฐœ์˜ ์ค„์— Ar,c (-1 โ‰ค Ar,c โ‰ค 1,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
๊ณต๊ธฐ์ฒญ์ •๊ธฐ๊ฐ€ ์„ค์น˜๋œ ๊ณณ์€ Ar,c๊ฐ€ -1์ด๊ณ , ๋‚˜๋จธ์ง€ ๊ฐ’์€ ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์ด๋‹ค.
-1์€ 2๋ฒˆ ์œ„์•„๋ž˜๋กœ ๋ถ™์–ด์ ธ ์žˆ๊ณ , ๊ฐ€์žฅ ์œ— ํ–‰, ์•„๋žซ ํ–‰๊ณผ ๋‘ ์นธ์ด์ƒ ๋–จ์–ด์ ธ ์žˆ๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ์งธ ์ค„์— T์ดˆ๊ฐ€ ์ง€๋‚œ ํ›„ ๊ตฌ์‚ฌ๊ณผ ๋ฐฉ์— ๋‚จ์•„์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์„ ์ถœ๋ ฅํ•œ๋‹ค.
"""

import sys
input = sys.stdin.readline

# r X c ๊ฒฉ์žํŒ, T์ดˆ
r, c, t = map(int, input().split())
arr = []

for i in range(r) :
    arr.append(list(map(int, input().split())))

up = -1
down = -1

# ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์œ„์น˜
for i in range(r) :
    # ๊ณต๊ธฐ์ฒญ์ •๊ธฐ๊ฐ€ ์„ค์น˜๋œ ๊ณณ์€ Ar,c๊ฐ€ -1
    if arr[i][0] == -1 :
        up = i
        down = i + 1
        break

# ๋ฏธ์„ธ๋จผ์ง€ ํ™•์‚ฐ
def spread() :
    # ์‚ฌ๋ฐฉ์œผ๋กœ ํ™•์‚ฐ๋œ๋‹ค
    dx = [-1, 0, 0, 1]
    dy = [0, -1, 1, 0]

    # ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ํ™•์‚ฐ๋œ temp
    temp = [[0] * c for _ in range(r)]

    for i in range(r) :
        for j in range(c) :
            # ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด!
            if arr[i][j] != 0 and arr[i][j] != -1 :
                tmp = 0
                for k in range(4):
                    nx = i + dx[k]
                    ny = j + dy[k]

                    if 0 <= nx < r and 0 <= ny < c and arr[nx][ny] != -1 :
                        # ํ™•์‚ฐ๋˜๋Š” ์–‘์€ Ar,c/5์ด๊ณ  ์†Œ์ˆ˜์ ์€ ๋ฒ„๋ฆฐ๋‹ค
                        temp[nx][ny] += arr[i][j] // 5
                        tmp += arr[i][j] // 5
                # (r, c)์— ๋‚จ์€ ๋ฏธ์„ธ๋จผ์ง€์˜ ์–‘์€ Ar,c - (Ar,c/5)ร—(ํ™•์‚ฐ๋œ ๋ฐฉํ–ฅ์˜ ๊ฐœ์ˆ˜)
                arr[i][j] -= tmp

    for i in range(r) :
        for j in range(c) :
            arr[i][j] += temp[i][j]

# ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์œ„์ชฝ
def air_up() :
    dx = [0, -1, 0, 1]
    dy = [1, 0, -1, 0]
    direct = 0
    before = 0

    # ํ–‰, 1์—ด
    x, y = up, 1

    while True :
        nx = x + dx[direct]
        ny = y + dy[direct]

        if x == up and y == 0 :
            break
        if nx < 0 or nx >= r or ny < 0 or ny >= c :
            direct += 1
            continue
        arr[x][y], before = before, arr[x][y]
        x = nx
        y = ny

# ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์•„๋ž˜
def air_down() :
    dx = [0, 1, 0, -1]
    dy = [1, 0, -1, 0]
    direct = 0
    before = 0

    # ํ–‰, 1์—ด
    x, y = down, 1
    while True :
        nx = x + dx[direct]
        ny = y + dy[direct]
        if x == down and y == 0 :
            break
        if nx < 0 or nx >= r or ny < 0 or ny >= c :
            direct += 1
            continue
        arr[x][y], before = before, arr[x][y]
        x = nx
        y = ny

for _ in range(t) :
    spread()
    air_up()
    air_down()

answer = 0
for i in range(r) :
    for j in range(c) :
        if arr[i][j] > 0 :
            answer += arr[i][j]

print(answer)

# 7 8 1
# 0 0 0 0 0 0 0 9
# 0 0 0 0 3 0 0 8
# -1 0 5 0 0 0 22 0
# -1 8 0 0 0 0 0 0
# 0 0 0 0 0 10 43 0
# 0 0 5 0 15 0 0 0
# 0 0 40 0 0 0 20 0
# 188


3. ํ•ด์„ค

This post is licensed under CC BY 4.0 by the author.