Post

🐣 미세먼지 안녕!

1. 문제 링크

17144번: 미세먼지 안녕!



2. 코드

Python3608ms31892KB
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
import sys
import copy

input = sys.stdin.readline
r, c, t = map(int, input().split())

graph = []
machine = [] # 공기청정기 좌표
dx = [0, 0, 1, -1] # 확산 방향 벡터
dy = [-1, 1, 0, 0]
left_x = [1, 0, -1, 0] # 반시계방향 회전 방향 벡터
left_y = [0, -1, 0, 1]
right_x = [1, 0, -1, 0] # 시계방향 회전 방향 벡터
right_y = [0, 1, 0, -1]

for i in range(r):
    graph.append(list(map(int, input().split())))
    if (graph[i][0] == -1):
        machine.append(i)


def spread(g): # 확산
    new_graph = [[0] * c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            if g[i][j] == -1:
                new_graph[i][j] = -1
                continue
            if g[i][j]:
                unit = g[i][j] // 5
                for k in range(4):
                    ny, nx = i + dy[k], j + dx[k]
                    if 0 <= ny < r and 0 <= nx < c and g[ny][nx] != -1:
                        new_graph[ny][nx] += unit
                        g[i][j] -= unit
                new_graph[i][j] += g[i][j]
    return new_graph


def clean(g): # 공기 청정기 순환
    new_graph = copy.deepcopy(g) # 기존 먼지 위치 카피

    y, x = machine[0], 0 # 반시계 방향 순환(공기청정기 위쪽 순환)
    g[y][x] = 0 # 공기 청정기에서 바람 불때도 0으로 기록하기 위함
    for i in range(4):
        ny, nx = y + left_y[i], x + left_x[i]
        while 0 <= ny < r and 0 <= nx < c and (ny != machine[0] or nx != 0):
            new_graph[ny][nx] = g[y][x]
            y, x = ny, nx
            ny, nx = y + left_y[i], x + left_x[i]

    y, x = machine[1], 0 # 시계 방향 순환(공기청정기 아래쪽 순환)
    g[y][x] = 0
    for i in range(4):
        ny, nx = y + right_y[i], x + right_x[i]
        while 0 <= ny < r and 0 <= nx < c and (ny != machine[1] or nx != 0):
            new_graph[ny][nx] = g[y][x]
            y, x = ny, nx
            ny, nx = y + right_y[i], x + right_x[i]
    return new_graph


for i in range(t):
    graph = spread(graph)
    graph = clean(graph)

total = 0
for i in range(r):
    for j in range(c):
        total += graph[i][j]
print(total + 2)



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