Posts [백준] #17144 미세먼지 안녕! Python (파이썬)
Post
Cancel

[백준] #17144 미세먼지 안녕! Python (파이썬)

미세먼지 안녕!

문제링크

https://www.acmicpc.net/problem/17144

코드

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
import sys
sys.stdin = open("input.txt",'r')
r,c,t = map(int,input().split())
board = [[int(x) for x in input().split()] for _ in range(r)]

# 공기 청정기 위치 찾기
flag = 0
for i in range(r):
    for j in range(c):
        if board[i][j] == -1:
            machine = [[i,j],[i+1,j]]
            flag = 1
            break
    if flag == 1:
        break

def spread(board):
    new_board = [[0]*c for _ in range(r)]
    for i in range(r):
        for j in range(c):
            if board[i][j] != 0 and board[i][j] != -1:
                temp = 0
                for k in range(4):
                    dx, dy = dir[k]
                    if -1 < i+dx < r and -1 < j+dy < c and board[i+dx][j+dy] != -1:
                        new_board[i+dx][j+dy] += board[i][j]//5
                        temp += 1
                new_board[i][j] += board[i][j]
                new_board[i][j] -= temp*(board[i][j]//5)
            if board[i][j] == -1:
                new_board[i][j] = -1

    return new_board

def move(board):
    up_x, up_y = machine[0]
    down_x, down_y = machine[1]
    new_board = [[0]*c for _ in range(r)]
    # 위에부분

    for j in range(2,c):
        new_board[up_x][j] = board[up_x][j-1]
    new_board[up_x][1] = 0
    for i in range(0,up_x):
        new_board[i][-1] = board[i+1][-1]
        if i != 0:
            for j in range(1,c-1):
                new_board[i][j] = board[i][j]
    for j in range(0,c-1):
        new_board[0][j] = board[0][j+1]
    for i in range(1,up_x):
        new_board[i][0] = board[i-1][0]


    # 아래부분
    for j in range(2,c):
        new_board[down_x][j] = board[down_x][j-1]
    new_board[down_x][1] = 0
    for i in range(down_x+1, r):
        new_board[i][-1] = board[i-1][-1]
    for j in range(c-1):
        new_board[-1][j] = board[-1][j+1]
    for i in range(down_x+1,r-1):
        new_board[i][0] = board[i+1][0]
        for j in range(1,c-1):
            new_board[i][j] = board[i][j]
 
    return new_board

dir = [[1,0],[0,1],[-1,0],[0,-1]]

for _ in range(t):

    # 미세 먼지의 확산
    
    mid_board = spread(board)

    board = move(mid_board)


ans = 0
print(sum(map(sum, board)))
This post is licensed under CC BY 4.0 by the author.

[백준] #17837 새로운 게임2 Python (파이썬)

[OS] 뮤텍스와 세마포어 (Mutex and Semaphore)

Loading comments from Disqus ...