본문 바로가기
Algorithm/Simulation

[자바] SWEA 2382 : 미생물 격리 (test)

by 베짱이28호 2025. 3. 9.

[자바] SWEA 2382 : 미생물 격리 (test)


풀이

방향성 생각

  • 단순 구현 문제
  • 백준에 마법사 상어와 복제와 뭔가 비슷한 느낌이다.
  • 동일 좌표에 어려 미생물 군집이 들어오는 경우가 있어서, 딕셔너리를 사용해서 합쳐줬다.

 

전체코드

from collections import deque

inside = lambda x,y : 0<x<N-1 and 0<y<N-1
border = lambda x,y : x in (0,N-1) or y in (0,N-1)
dires = [(0,0),(0,-1),(0,1),(-1,0),(1,0)] # [x, 상 하 좌 우 ]

# border에 가서 반대 방향이 되는 겅우, 상하좌우 방향 인덱스가 정해져있어서 잘 체크하기
def reflect(dires):
    if dires==1: return 2
    elif dires==2: return 1
    elif dires==3: return 4
    elif dires==4: return 3


def move(Q):
    global answer
    nQ = deque()
    temp = {}
    while Q:
        x,y,cnt,dire,t = Q.popleft()

        # 다 도달했으면 개수 카운팅해주기
        if t==M:
            answer += cnt
            continue

        dx,dy = dires[dire]
        nx,ny = x+dx,y+dy

        # 내부에서 이동하는 경우 -> 해당 좌표에 미생물이 있는지 체크
        if inside(nx,ny):
            if (nx,ny) not in temp:
                temp[(nx,ny)] = [[cnt],[dire],t+1]
            else:
                temp[(nx,ny)][0].append(cnt)
                temp[(nx,ny)][1].append(dire)
        # 약품 영역에서는 서로 겹치는 경우가 없기 떄문에 미생물이 절반으로 줄어드는 경우만 체크한다.
        elif border(nx,ny) and cnt//2:
            temp[(nx,ny)] = ([cnt//2],[reflect(dire)],t+1)

    for (x,y),(cnts,diress,t) in temp.items():
        nQ.append((x,y,sum(cnts),diress[cnts.index(max(cnts))],t))
    return nQ

TC = int(input())
for tc in range(1,TC+1):

    N,M,K = map(int,input().split())

    # 미생물들을 큐에 담고 시작
    Q = deque()
    for _ in range(K):
        y,x,cnt,dire = map(int,input().split())
        Q.append((x,y,cnt,dire,0)) # x,y,cnt,dire,t

    # M번에 이터레이션 이후 M=1번의 이터레이션에서는 개수를 카운팅해준다.
    answer = 0
    for i in range(M+1):
        Q = move(Q)
    print(f"#{tc} {answer}")

코멘트

  • .

댓글