코딩테스트/BOJ

[백준] 18405. 경쟁적 전염

박소민 2023. 4. 1. 13:42
18405. 경쟁적 전염
 

18405번: 경쟁적 전염

첫째 줄에 자연수 N, K가 공백을 기준으로 구분되어 주어진다. (1 ≤ N ≤ 200, 1 ≤ K ≤ 1,000) 둘째 줄부터 N개의 줄에 걸쳐서 시험관의 정보가 주어진다. 각 행은 N개의 원소로 구성되며, 해당 위치

www.acmicpc.net

 

  • 내 풀이
# 1~k번
#s초 후 x,y에 들어있는 바이러스 종류 출력
#번호 낮은것부터 먼저 증식
# 없으면 0
from collections import deque

def bfs():
    global s
    while queue:
        
        virus,x1,y1=queue.popleft()
        if visited[x1][y1]>=s: #s초 까지만 진행
            return
          
        for i in range(4):
            nx=x1+dx[i]
            ny=y1+dy[i]
            if 0<=nx<n and 0<=ny<n:
                #원래 바이러스 있는 곳
                if graph[nx][ny]!=0 and visited[nx][ny]==0: continue
                  
                if visited[nx][ny]==0:
                    if graph[nx][ny]==0: #처음 방문하는 경우
                        graph[nx][ny]=virus
                        visited[nx][ny]=visited[x1][y1]+1
                        
                #이미 한번 방문된 경우
                elif visited[nx][ny]<visited[x1][y1]+1: continue
                else: #같으면 더 작은 값
                    graph[nx][ny]=min(graph[nx][ny], virus)
                      
                queue.append((graph[nx][ny],nx,ny))


n,k= map(int,input().split())
graph=[]
dx=[-1,1,0,0]
dy=[0,0,-1,1]
visited=[[0 for _ in range(n)] for _ in range(n)]
for _ in range(n):
    graph.append(list(map(int,input().split())))

s,x,y=map(int,input().split())
queue=deque()
for i in range(n):
    for j in range(n):
        if graph[i][j]==0:continue
        queue.append((graph[i][j],i,j))

bfs()

print(graph[x-1][y-1])