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
R, C, T = map(int, input().split())
room = []
air_cleaner = []
for i in range(R):
    line = list(map(int, input().split()))
    if -1 in line:
        air_cleaner.append(i)
    room.append(line)

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

# ๋ฏธ์„ธ๋จผ์ง€ ํ™•์‚ฐ
def diffusion(x, y):
    count = 0
    move_amount = room[x][y] // 5
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
        # ๋ฒฝ์— ์•ˆ ๋ถ€๋”ชํžˆ๊ณ  ๊ณต๊ธฐ ์ฒญ์ •๊ธฐ ์•„๋‹ ์‹œ ํ™•์‚ฐ
        if 0 <= nx < R and 0 <= ny < C and room[nx][ny] != -1:
            count += 1
            dust[nx][ny][1] += move_amount  # ํ™•์‚ฐ
    dust[x][y][0] = move_amount * count # ํ™•์‚ฐ๋˜๊ณ  ์ค„์€ ์–‘

# ๊ณต๊ธฐ์ฒญ์ •๊ธฐ ์ž‘๋™
def air_clean():
    # ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ 
    for i in range(air_cleaner[0], 0, -1):
        room[i][0], room[i-1][0] = room[i-1][0], room[i][0]
    for i in range(0, C-1):
        room[0][i], room[0][i+1] = room[0][i+1], room[0][i]
    for i in range(0, air_cleaner[0]):
        room[i][C-1], room[i+1][C-1] = room[i+1][C-1], room[i][C-1]
    for i in range(C-1, 0, -1):
        room[air_cleaner[0]][i], room[air_cleaner[0]][i-1] = room[air_cleaner[0]][i-1], room[air_cleaner[0]][i]
    room[air_cleaner[0]][1] = 0     # ๊นจ๋—ํ•œ ๋ฐ”๋žŒ
    
    # ์‹œ๊ณ„ ๋ฐฉํ–ฅ
    for i in range(air_cleaner[1], R-1):
        room[i][0], room[i+1][0] = room[i+1][0], room[i][0]
    for i in range(0, C-1):
        room[R-1][i], room[R-1][i+1] = room[R-1][i+1], room[R-1][i]
    for i in range(R-1, air_cleaner[1], -1):
        room[i][C-1], room[i-1][C-1] =  room[i-1][C-1], room[i][C-1]
    for i in range(C-1, 0, -1):
        room[air_cleaner[1]][i], room[air_cleaner[1]][i-1] = room[air_cleaner[1]][i-1], room[air_cleaner[1]][i]
    room[air_cleaner[1]][1] = 0     # ๊นจ๋—ํ•œ ๋ฐ”๋žŒ

while T:
    T -= 1
    # ์ค„์–ด๋“ค ๋จผ์ง€ ์–‘, ๋Š˜์–ด๋‚  ๋จผ์ง€ ์–‘
    dust = [[[0, 0] for _ in range(C)] for _ in range(R)]
    # ๋ฏธ์„ธ๋จผ์ง€ ์ด๋™ ํ™•์ธ
    for i in range(R):
        for j in range(C):
            if room[i][j] > 0:
                diffusion(i, j)
    # ๋ฏธ์„ธ๋จผ์ง€ ํ™•์‚ฐ ์ •๋ฆฌ
    for i in range(R):
        for j in range(C):
            # ๋ณ€๋™์‚ฌํ•ญ์ด ์žˆ์„ ๋•Œ
            if dust[i][j] != [0, 0]:
                room[i][j] -= dust[i][j][0]
                room[i][j] += dust[i][j][1]
    air_clean()

# ๋‚จ์•„์žˆ๋Š” ๋ฏธ์„ธ๋จผ์ง€ ์–‘
result = 0
for i in range(R):
    for j in range(C):
        if room[i][j] > 0:
            result += room[i][j]
print(result)


3. ํ•ด์„ค

์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

  1. ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ํ™•์‚ฐ๋  ๋•Œ ๋ชจ๋“  ๋ฐฉ์˜ ๋ฏธ์„ธ๋จผ์ง€๊ฐ€ ํ™•์‚ฐ๋˜๊ธฐ ์ „์— ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ์ตœ์ข… ๊ฐ’์ด ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋ฏ€๋กœ dust 3์ฐจ์› ๋ฐฐ์—ด์— ๊ฐ ์œ„์น˜์— ์ค„์–ด๋“ค ๋จผ์ง€์–‘, ๋Š˜์–ด๋‚  ๋จผ์ง€์–‘ ๊ฐ’์„ ๋‹ด์•„๋‘์—ˆ๋‹ค.

  2. ๊ณต๊ธฐ ์ฒญ์ •๊ธฐ๊ฐ€ ๋Œ ๋•Œ ๋„๋Š” ๋ฐฉํ–ฅ๊ณผ ๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๊ฐ’์„ ์Šค์œ„์นญ ํ•ด์ฃผ์—ˆ๋‹ค. ํ•œ ๋ฐ”ํ€ด๋ฅผ ๋Œ์•˜์œผ๋ฉด ๊ณต๊ธฐ์ฒญ์ •๊ธฐ์˜ ๋ฐ”๋žŒ ์‹œ์ž‘ ์œ„์น˜์˜ ๊ฐ’๋งŒ 0์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. (๊ณต๊ธฐ ์ฒญ์ •๊ธฐ์—์„œ ๋‚˜์˜ค๋Š” ๋ฐ”๋žŒ์€ ๊นจ๋—ํ•œ ๋ฐ”๋žŒ์ด๋ฏ€๋กœ)

์œ„์˜ ๊ณผ์ •์„ T ์‹œ๊ฐ„๋™์•ˆ ๋ฐ˜๋ณตํ•ด์ค€๋‹ค.

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