본문 바로가기
알고리즘/BOJ

[BOJ / 백준] 17144 미세먼지 안녕! python / 파이썬 구현

by seohmoon 2023. 6. 10.

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

 

17144번: 미세먼지 안녕!

미세먼지를 제거하기 위해 구사과는 공기청정기를 설치하려고 한다. 공기청정기의 성능을 테스트하기 위해 구사과는 집을 크기가 R×C인 격자판으로 나타냈고, 1×1 크기의 칸으로 나눴다. 구사

www.acmicpc.net


1) 요구사항 분석 : 문제 읽기

방의 정보가 주어졌을 때, T초가 지난 후 구사과의 방에 남아있는 미세먼지의 양

 

2) 설계 : 접근 방식

우선 미세먼지가 확산된 다음 -> 공기청정기가 작동하게 하였다.

미세먼지를 확신시킬때, 공기청정기의 위치를 air라는 배열에 기록하였다.

 

그리고 공기청정기 작동은 위는 반시계방향

그 다음 아래는 시계방향으로 돌아가게 해줬다.

import sys

input = sys.stdin.readline
r, c, t = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(r)]
di = [0, 1, 0, -1]
dj = [1, 0, -1, 0]

def in_range(x, y):
    return 0 <= x < r and 0 <= y < c

for tc in range(t):
    ans = [[0 for _ in range(c)] for _ in range(r)]
    air = []
    # 미세먼지 확산
    for i in range(r):
        for j in range(c):
            if arr[i][j] == -1: # 공기청정기 자리
                ans[i][j] = -1
                air.append(i)
                air.append(j)
                continue
            cnt = 0
            for a in range(4):
                ni = di[a] + i
                nj = dj[a] + j
                if in_range(ni, nj) and arr[ni][nj] != -1:
                    ans[ni][nj] += arr[i][j] // 5
                    cnt += 1
            ans[i][j] += arr[i][j] - ((arr[i][j] // 5) * cnt)
    x = air[0]
    y = air[1] + 1
    tem = 0

    # 공기청정기 작동
    while True: # 오른쪽으로 하나씩 밀기
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            y += 1
        else:
            break
    y -= 1
    x -= 1

    while True: # 위로 하나씩 밀기
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            x -= 1
        else:
            break
    x += 1
    y -= 1

    while True: # 왼쪽으로 하나씩 밀기
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            y -= 1
        else:
            break
    y += 1
    x += 1

    while x != air[0]: # 아래
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            x += 1
        else:
            break
    
    while ans[x][y] != -1: # 다시 왼쪽으로 공기청정기 찾을 때 까지
        tem2 = ans[x][y]
        ans[x][y] = tem
        tem = tem2
        y += 1

    x = air[2]
    y = air[3] + 1
    tem = 0
    
    while True:  # 오른쪽으로 하나씩 밀기
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            y += 1
        else:
            break
    y -= 1
    x += 1
  
    while True: # 아래
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            x += 1
        else:
            break
    x -= 1
    y -= 1

    while True: # 왼쪽으로 하나씩 밀기
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            y -= 1
        else:
            break
    y += 1
    x -= 1

    while x > air[2]: # 위로 하나씩 밀기
        if in_range(x, y):
            tem2 = ans[x][y]
            ans[x][y] = tem
            tem = tem2
            x -= 1
        else:
            break

    while ans[x][y] != -1: # 다시 왼쪽으로 공기청정기 찾을 때 까지
        tem2 = ans[x][y]
        ans[x][y] = tem
        tem = tem2
        y += 1
    
    arr = ans

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

print(answer)

댓글