1+ #include < bits/stdc++.h>
2+
3+ using namespace std ;
4+
5+ class Virus {
6+ public:
7+ int index;
8+ int second;
9+ int x;
10+ int y;
11+ Virus (int index, int second, int x, int y) {
12+ this ->index = index;
13+ this ->second = second;
14+ this ->x = x;
15+ this ->y = y;
16+ }
17+ // 정렬 기준은 '번호가 낮은 순서'
18+ bool operator <(Virus &other) {
19+ return this ->index < other.index ;
20+ }
21+ };
22+
23+ int n, k;
24+ // 전체 보드 정보를 담는 배열
25+ int graph[200 ][200 ];
26+ // 바이러스에 대한 정보를 담는 리스트
27+ vector<Virus> viruses;
28+
29+ // 바이러스가 퍼져나갈 수 있는 4가지의 위치
30+ int dx[] = {-1 , 0 , 1 , 0 };
31+ int dy[] = {0 , 1 , 0 , -1 };
32+
33+ int main (void ) {
34+ cin >> n >> k;
35+
36+ // 보드 정보를 한 줄 단위로 입력
37+ for (int i = 0 ; i < n; i++) {
38+ for (int j = 0 ; j < n; j++) {
39+ cin >> graph[i][j];
40+ // 해당 위치에 바이러스가 존재하는 경우
41+ if (graph[i][j] != 0 ) {
42+ // (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입
43+ viruses.push_back (Virus (graph[i][j], 0 , i, j));
44+ }
45+ }
46+ }
47+
48+ // 정렬 이후에 큐로 옮기기 (낮은 번호의 바이러스가 먼저 증식하므로)
49+ sort (viruses.begin (), viruses.end ());
50+ queue<Virus> q;
51+ for (int i = 0 ; i < viruses.size (); i++) {
52+ q.push (viruses[i]);
53+ }
54+
55+ int target_s, target_x, target_y;
56+ cin >> target_s >> target_x >> target_y;
57+
58+ // 너비 우선 탐색(BFS) 진행
59+ while (!q.empty ()) {
60+ Virus virus = q.front ();
61+ q.pop ();
62+ // 정확히 second만큼 초가 지나거나, 큐가 빌 때까지 반복
63+ if (virus.second == target_s) break ;
64+ // 현재 노드에서 주변 4가지 위치를 각각 확인
65+ for (int i = 0 ; i < 4 ; i++) {
66+ int nx = virus.x + dx[i];
67+ int ny = virus.y + dy[i];
68+ // 해당 위치로 이동할 수 있는 경우
69+ if (0 <= nx && nx < n && 0 <= ny && ny < n) {
70+ // 아직 방문하지 않은 위치라면, 그 위치에 바이러스 넣기
71+ if (graph[nx][ny] == 0 ) {
72+ graph[nx][ny] = virus.index ;
73+ q.push (Virus (virus.index , virus.second + 1 , nx, ny));
74+ }
75+ }
76+ }
77+ }
78+
79+ cout << graph[target_x - 1 ][target_y - 1 ] << ' \n ' ;
80+ }
0 commit comments