1+ import java .util .*;
2+
3+ class Virus implements Comparable <Virus > {
4+
5+ private int index ;
6+ private int second ;
7+ private int x ;
8+ private int y ;
9+
10+ public Virus (int index , int second , int x , int y ) {
11+ this .index = index ;
12+ this .second = second ;
13+ this .x = x ;
14+ this .y = y ;
15+ }
16+
17+ public int getIndex () {
18+ return this .index ;
19+ }
20+
21+ public int getSecond () {
22+ return this .second ;
23+ }
24+
25+ public int getX () {
26+ return this .x ;
27+ }
28+
29+ public int getY () {
30+ return this .y ;
31+ }
32+
33+ // 정렬 기준은 '번호가 낮은 순서'
34+ @ Override
35+ public int compareTo (Virus other ) {
36+ if (this .index < other .index ) {
37+ return -1 ;
38+ }
39+ return 1 ;
40+ }
41+ }
42+
43+ public class Main {
44+
45+ public static int n , k ;
46+ // 전체 보드 정보를 담는 배열
47+ public static int [][] graph = new int [200 ][200 ];
48+ public static ArrayList <Virus > viruses = new ArrayList <Virus >();
49+
50+ // 바이러스가 퍼져나갈 수 있는 4가지의 위치
51+ public static int [] dx = {-1 , 0 , 1 , 0 };
52+ public static int [] dy = {0 , 1 , 0 , -1 };
53+
54+ public static void main (String [] args ) {
55+ Scanner sc = new Scanner (System .in );
56+
57+ n = sc .nextInt ();
58+ k = sc .nextInt ();
59+
60+ for (int i = 0 ; i < n ; i ++) {
61+ for (int j = 0 ; j < n ; j ++) {
62+ graph [i ][j ] = sc .nextInt ();
63+ // 해당 위치에 바이러스가 존재하는 경우
64+ if (graph [i ][j ] != 0 ) {
65+ // (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입
66+ viruses .add (new Virus (graph [i ][j ], 0 , i , j ));
67+ }
68+ }
69+ }
70+
71+ // 정렬 이후에 큐로 옮기기 (낮은 번호의 바이러스가 먼저 증식하므로)
72+ Collections .sort (viruses );
73+ Queue <Virus > q = new LinkedList <Virus >();
74+ for (int i = 0 ; i < viruses .size (); i ++) {
75+ q .offer (viruses .get (i ));
76+ }
77+
78+ int targetS = sc .nextInt ();
79+ int targetX = sc .nextInt ();
80+ int targetY = sc .nextInt ();
81+
82+ // 너비 우선 탐색(BFS) 진행
83+ while (!q .isEmpty ()) {
84+ Virus virus = q .poll ();
85+ // 정확히 second만큼 초가 지나거나, 큐가 빌 때까지 반복
86+ if (virus .getSecond () == targetS ) break ;
87+ // 현재 노드에서 주변 4가지 위치를 각각 확인
88+ for (int i = 0 ; i < 4 ; i ++) {
89+ int nx = virus .getX () + dx [i ];
90+ int ny = virus .getY () + dy [i ];
91+ // 해당 위치로 이동할 수 있는 경우
92+ if (0 <= nx && nx < n && 0 <= ny && ny < n ) {
93+ // 아직 방문하지 않은 위치라면, 그 위치에 바이러스 넣기
94+ if (graph [nx ][ny ] == 0 ) {
95+ graph [nx ][ny ] = virus .getIndex ();
96+ q .offer (new Virus (virus .getIndex (), virus .getSecond () + 1 , nx , ny ));
97+ }
98+ }
99+ }
100+ }
101+
102+ System .out .println (graph [targetX - 1 ][targetY - 1 ]);
103+ }
104+ }
0 commit comments