Skip to content

Commit

Permalink
#17 17144번 미세먼지 안녕!
Browse files Browse the repository at this point in the history
  • Loading branch information
leejw-lu committed Dec 29, 2023
1 parent 22289aa commit 3fa14c1
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions 시뮬레이션/17144_미세먼지 안녕!.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import sys
read=sys.stdin.readline

dx = [0,-1,0,1] #동북서남 (x,y 방향 주의!!!!)
dy = [1,0,-1,0]
airCleaner=[]
result=0

#r과 c 범위가 매우 작기 때문에 미리 먼지 위치 append 안해도 된다.
#1. 미세먼지 확산 -> "동시성"
def spread():
temp=[[0]*c for _ in range(r)]
for i in range(r):
for j in range(c):
if graph[i][j]!=0 and graph[i][j]!=-1:
dust=0
for k in range(4):
nx=i+dx[k]
ny=j+dy[k]
if 0<=nx<r and 0<=ny<c and graph[nx][ny]!=-1:
temp[nx][ny]+=graph[i][j]//5
dust+=graph[i][j]//5
graph[i][j]-=dust
#graph+ temp 배열 합치기 (동시성)
for i in range(r):
for j in range(c):
graph[i][j]+=temp[i][j]

#2. 공기청정기 위쪽: 동->북->서->남
def clean_up():
d=0 #동 부터 시작
before=0
x,y=airCleaner[0],1 #로봇 다음칸

while True:
nx=x+dx[d]
ny=y+dy[d]
if nx<0 or nx>=r or ny<0 or ny>=c:
d=(d+1)%4
continue
if x==airCleaner[0] and y==0:
break

graph[x][y],before = before,graph[x][y]
x,y=nx,ny

#3. 공기청정기 아래쪽: 동->남->서->북
def clean_down():
d=0 #동 부터 시작
before=0
x,y=airCleaner[1],1 #로봇 다음칸

while True:
nx=x+dx[d]
ny=y+dy[d]
if nx<0 or nx>=r or ny<0 or ny>=c:
d=(d-1)%4
continue
if x==airCleaner[1] and y==0:
break

graph[x][y],before = before,graph[x][y]
x,y=nx,ny

#___________________main
r,c,t=map(int,read().split())
graph=[list(map(int,read().split())) for _ in range(r)]

#공기청정기 위치 찾기
for i in range(r):
if graph[i][0]==-1:
airCleaner.append(i)

for _ in range(t):
spread()
clean_up()
clean_down()

#미세먼지 출력
for i in range(r):
for j in range(c):
if graph[i][j]>0:
result+=graph[i][j]
print(result)

1 comment on commit 3fa14c1

@leejw-lu
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

문제 이해하는 건 쉬웠는데 구현하는게 ...까다로웠다.
dx, dy 방향 헷갈려ㅕ.... 항상 시뮬좌표 생각해보고 방향 잡아야겠다. 방향땜에 시간 많이 잡아먹음.....

Please sign in to comment.