티스토리 뷰
문제
https://www.acmicpc.net/problem/17144
풀이
공기청정기의 위쪽 부분과 아래쪽부분을 나눠서 생각하였음.
한 칸씩 이동하는 것이 아닌, 각 행마다 직접 값을 넣어서 new라는 배열에 추가해주었음.
위쪽 공기순환을 우선 보면,
첫 번째 행은 가장 왼쪽의 원소만 아래로 내려가고 다음 행의 마지막 원소만 끝에 추가해주면 첫 번째 행이 완성됨.
이렇게 첫번째와 마지막 행만 따로 구현하고, 가운데 부분은 다 동일하게 끝부분만 이동하므로 세 경우로 나눠서 구현하였음.
이때 주의할 것은 미세먼지가 퍼지는 것이 차례대로 진행되면 안되고, 동시에 진행되어야함. 그래서 dust_temp라는 배열을 이용해 한 번에 담아둔다음에 배열을 수정하였음.
import sys
input = sys.stdin.readline
r,c,t = map(int,input().split())
dust = []
cleaner = []
d = [(1,0),(0,1),(-1,0),(0,-1)]
for i in range(r):
dust.append(list(map(int,input().split())))
for j in range(len(dust[i])):
if dust[i][j] == -1:
cleaner.append((i,j))
def spread(x,y):
global dust, dust_temp
value = 0
for i in range(4):
nx = x + d[i][0]
ny = y + d[i][1]
if nx < 0 or ny < 0 or nx >= r or ny >= c:
continue
if dust[nx][ny] == -1:
continue
value += int(dust[x][y] / 5)
dust_temp.append((nx,ny,int(dust[x][y] / 5)))
dust[x][y] -= value
def clean():
global dust
new = []
start = cleaner[0]
for i in range(start[0] + 1):
if i == 0:
new.append(dust[0][1:] + [dust[i + 1][-1]])
elif i == start[0]:
new.append([-1,0] + dust[start[0]][1:c - 1])
else:
new.append([dust[i - 1][0]] + dust[i][1:c - 1] + [dust[i + 1][-1]])
start = cleaner[1]
for i in range(start[0],r):
if i == start[0]:
new.append([-1,0] + dust[start[0]][1:c - 1])
elif i == r - 1:
new.append(dust[r - 1][1:] + [dust[r - 2][-1]])
else:
new.append([dust[i + 1][0]] + dust[i][1:c - 1] + [dust[i - 1][-1]])
dust = new
def checkDust():
count = 0
for i in range(r):
for j in range(c):
if dust[i][j] > 0:
count += dust[i][j]
print(count)
for _ in range(t):
dust_temp = []
for i in range(r):
for j in range(c):
if dust[i][j] > 0:
spread(i,j)
for i in range(len(dust_temp)):
dust[dust_temp[i][0]][dust_temp[i][1]] += dust_temp[i][2]
clean()
checkDust()
'PS' 카테고리의 다른 글
[MySQL] 프로그래머스_SELECT (0) | 2023.04.02 |
---|---|
[Python] 백준_원판 돌리기(17822) (0) | 2023.03.24 |
[Python] 백준_이차원 배열과 연산(17140) (0) | 2023.03.22 |
[Python] 백준_마법사 상어와 파이어스톰(20058) (0) | 2023.03.22 |
[Python] 백준_드래곤커브(15685) (0) | 2023.03.22 |
댓글
최근에 올라온 글
- Total
- Today
- Yesterday