Skip to content

Commit c9da387

Browse files
committed
[Gold V] Title: 경쟁적 전염, Time: 232 ms, Memory: 22216 KB -BaekjoonHub
1 parent 0c6470e commit c9da387

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [Gold V] 경쟁적 전염 - 18405
2+
3+
[문제 링크](https://www.acmicpc.net/problem/18405)
4+
5+
### 성능 요약
6+
7+
메모리: 22216 KB, 시간: 232 ms
8+
9+
### 분류
10+
11+
너비 우선 탐색, 그래프 이론, 그래프 탐색, 구현
12+
13+
### 제출 일자
14+
15+
2025년 3월 21일 15:44:24
16+
17+
### 문제 설명
18+
19+
<p><em>N</em>x<em>N</em> 크기의 시험관이 있다. 시험관은 1x1 크기의 칸으로 나누어지며, 특정한 위치에는 바이러스가 존재할 수 있다. 모든 바이러스는 1번부터 <em>K</em>번까지의 바이러스 종류 중 하나에 속한다.</p>
20+
21+
<p>시험관에 존재하는 모든 바이러스는 1초마다 상, 하, 좌, 우의 방향으로 증식해 나간다. 단, 매 초마다 번호가 낮은 종류의 바이러스부터 먼저 증식한다. 또한 증식 과정에서 특정한 칸에 이미 어떠한 바이러스가 존재한다면, 그 곳에는 다른 바이러스가 들어갈 수 없다.</p>
22+
23+
<p>시험관의 크기와 바이러스의 위치 정보가 주어졌을 때, <em>S</em>초가 지난 후에 (X,Y)에 존재하는 바이러스의 종류를 출력하는 프로그램을 작성하시오. 만약 <em>S</em>초가 지난 후에 해당 위치에 바이러스가 존재하지 않는다면, 0을 출력한다. 이 때 X<font face="sans-serif, Arial, Verdana, Trebuchet MS, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol">와 </font>Y는 각각 행과 열의 위치를 의미하며, 시험관의 가장 왼쪽 위에 해당하는 곳은 (1,1)에 해당한다.</p>
24+
25+
<p>예를 들어 다음과 같이 3x3 크기의 시험관이 있다고 하자. 서로 다른 1번, 2번, 3번 바이러스가 각각 (1,1), (1,3), (3,1)에 위치해 있다. 이 때 2초가 지난 뒤에 (3,2)에 존재하는 바이러스의 종류를 계산해보자.</p>
26+
27+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/02958527-debb-46e3-a56d-79b87ad46d03/-/preview/" style="height: 148px; width: 250px;"></p>
28+
29+
<p style="text-align: justify;">1초가 지난 후에 시험관의 상태는 다음과 같다.</p>
30+
31+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/9ab631e7-1f1d-42fe-b8b8-95ded9bce52d/-/preview/" style="height: 148px; width: 250px;"></p>
32+
33+
<p>2초가 지난 후에 시험관의 상태는 다음과 같다.</p>
34+
35+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/35ea13c6-9ee3-40e1-8b21-c37beaf46c59/-/preview/" style="height: 148px; width: 250px;"></p>
36+
37+
<p style="text-align: justify;">결과적으로 2초가 지난 뒤에 (3,2)에 존재하는 바이러스의 종류는 3번 바이러스다. 따라서 3을 출력하면 정답이다.</p>
38+
39+
### 입력
40+
41+
<p>첫째 줄에 자연수 <em>N</em>, <em>K</em>가 공백을 기준으로 구분되어 주어진다. (1 ≤ <em>N</em> ≤ 200, 1 ≤ <em>K</em> ≤ 1,000) 둘째 줄부터 <em>N</em>개의 줄에 걸쳐서 시험관의 정보가 주어진다. 각 행은 <em>N</em>개의 원소로 구성되며, 해당 위치에 존재하는 바이러스의 번호가 공백을 기준으로 구분되어 주어진다. 단, 해당 위치에 바이러스가 존재하지 않는 경우 0이 주어진다. 또한 모든 바이러스의 번호는 <em>K</em>이하의 자연수로만 주어진다. <em>N</em>+2번째 줄에는 <em>S</em>, <em>X</em>, <em>Y</em>가 공백을 기준으로 구분되어 주어진다. (0 ≤ <em>S</em> ≤ 10,000, 1 ≤ <em>X</em>, <em>Y</em> ≤ <em>N</em>)</p>
42+
43+
### 출력
44+
45+
<p><em>S</em>초 뒤에 (X,Y)에 존재하는 바이러스의 종류를 출력한다. 만약 <em>S</em>초 뒤에 해당 위치에 바이러스가 존재하지 않는다면, 0을 출력한다.</p>
46+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
static class Node{
7+
int x;
8+
int y;
9+
int virus;
10+
int time;
11+
12+
public Node(int x, int y, int virus, int time) {
13+
this.x = x;
14+
this.y = y;
15+
this.virus = virus;
16+
this.time = time;
17+
}
18+
}
19+
20+
static final int[] dx = {0, 0, -1, 1};
21+
static final int[] dy = {1, -1, 0, 0};
22+
static int n, k, s;
23+
static int[][] map;
24+
static Queue<Node> q = new LinkedList<>();
25+
26+
public static void main(String[] args) throws IOException {
27+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
28+
StringTokenizer st = new StringTokenizer(br.readLine());
29+
30+
n = Integer.parseInt(st.nextToken());
31+
k = Integer.parseInt(st.nextToken());
32+
map = new int[n][n];
33+
ArrayList<Node> list = new ArrayList<>();
34+
35+
for (int i = 0; i < n; i++) {
36+
st = new StringTokenizer(br.readLine());
37+
for (int j = 0; j < n; j++) {
38+
map[i][j] = Integer.parseInt(st.nextToken());
39+
if (map[i][j] != 0) {
40+
list.add(new Node(i, j, map[i][j], 0));
41+
}
42+
}
43+
}
44+
45+
Collections.sort(list, new Comparator<Node>() {
46+
@Override
47+
public int compare(Node o1, Node o2) {
48+
return o1.virus - o2.virus;
49+
}
50+
});
51+
52+
for (Node node : list) {
53+
q.add(node);
54+
}
55+
56+
st = new StringTokenizer(br.readLine());
57+
s = Integer.parseInt(st.nextToken());
58+
int x = Integer.parseInt(st.nextToken());
59+
int y = Integer.parseInt(st.nextToken());
60+
61+
bfs();
62+
63+
System.out.println(map[x - 1][y - 1]);
64+
}
65+
66+
static void bfs() {
67+
68+
while (!q.isEmpty()) {
69+
Node now = q.poll();
70+
int x = now.x;
71+
int y = now.y;
72+
73+
if (now.time == s) {
74+
return;
75+
}
76+
77+
for (int k = 0; k < 4; k++) {
78+
int nx = x + dx[k];
79+
int ny = y + dy[k];
80+
81+
if (0 <= nx && nx < n && 0 <= ny && ny < n) {
82+
if (map[nx][ny] == 0) {
83+
map[nx][ny] = now.virus;
84+
q.add(new Node(nx, ny, now.virus, now.time + 1));
85+
}
86+
}
87+
}
88+
89+
}
90+
91+
}
92+
}

0 commit comments

Comments
 (0)