Jun's Development Journey
[BOJ] 14502번 연구소 본문
문제
https://www.acmicpc.net/problem/14502
14502번: 연구소
인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크기가 N×M인 직사각형으로 나타낼 수 있으며, 직사각형은 1×1 크기의 정사각형으로 나누어져 있다. 연구소는 빈 칸, 벽으로 이루어져 있으며, 벽은 칸 하나를 가득 차지한다. 일부 칸은 바이러스가 존재하며, 이 바이러스는 상하좌우로 인접한 빈 칸으로 모두 퍼져나갈 수 있다.
www.acmicpc.net
풀이
이 문제는 BruteForce와 DFS가 합쳐진 문제이다. 빈칸에 벽을 세울 수 있는 모든 경우의 수마다 바이러스가 퍼질 수 있는 공간을 채워가며 안전 영역을 구하면 된다.
벽을 세우는 경우의 수를 구하는 함수에서 바이러스 있는 곳의 좌표와 함께 사용되므로 4중 포문이 쓰여서 다소 깔끔하지 않고 효율성은 떨어지는 코드이지만 추후에 더 효율적인 방법을 알게 된다면 수정해서 올려야겠다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
class Point {
int row;
int col;
public Point(int row, int col) {
this.row = row;
this.col = col;
}
}
public class Main {
static Scanner scan = new Scanner(System.in);
static int[] r = { -1, 1, 0, 0 };
static int[] c = { 0, 0, -1, 1 };
static int[][] lab, tmp;
static Vector<Point> virus, safe;
static int Max = -1;
static int N, M;
static void init() {
N = scan.nextInt();
M = scan.nextInt();
lab = new int[N][M];
tmp = new int[N][M];
virus = new Vector<Point>();
safe = new Vector<Point>();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
lab[i][j] = scan.nextInt();
if (lab[i][j] == 2)
virus.add(new Point(i, j));
if (lab[i][j] == 0)
safe.add(new Point(i, j));
}
}
copy_lab();
}
static void install() {
for (int i = 0; i < len; i++) {
for (int j = 0; j < len; j++) {
if (i != j) {
for (int k = 0; k < len; k++) {
if (i != k && j != k) {
virus_dfs(virus.get(l).row, virus.get(l).col);
copy_lab();
}
}
}
}
}
}
static void virus_dfs(int row, int col) {
for (int i = 0; i < 4; i++) {
if (is_valid(row + r[i], col + c[i])) {
tmp[row + r[i]][col + c[i]] = 2;
virus_dfs(row + r[i], col + c[i]);
}
}
}
static boolean is_valid(int row, int col) {
if ((row >= 0 && row < N) && (col >= 0 && col < M) && tmp[row][col] != 1 && tmp[row][col] != 2)
return true;
return false;
}
static int get_safe() {
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (tmp[i][j] == 0)
cnt++;
}
}
return cnt;
}
static void copy_lab() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
tmp[i][j] = lab[i][j];
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
init();
install();
System.out.println(Max);
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'BOJ > DFS' 카테고리의 다른 글
[BOJ] 1260 DFS, BFS (0) | 2021.02.26 |
---|---|
[BOJ] 2644 촌수계산 (0) | 2021.02.23 |
[BOJ] 2606번 바이러스 (0) | 2021.02.18 |
[BOJ] 11724번 연결 요소의 갯수 (0) | 2019.08.30 |
[BOJ] 11403번 경로 찾기 (0) | 2019.08.30 |