This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ14502.java
More file actions
112 lines (91 loc) · 3.19 KB
/
Copy pathBOJ14502.java
File metadata and controls
112 lines (91 loc) · 3.19 KB
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
105
106
107
108
109
110
111
112
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class BOJ14502 {
static int n;
static int m;
static int[][] map;
static boolean[][] visited;
static int answer = 0;
static int[] xDir = new int[]{-1, 0, 1, 0};
static int[] yDir = new int[]{0, 1, 0, -1};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
map = new int[n + 2][m + 2];
visited = new boolean[n + 1][m + 1];
for (int i = 0; i <= n + 1; i++) {
Arrays.fill(map[i], 1);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
map[i][j] = sc.nextInt();
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dfs(i, j, 1);
}
}
System.out.println(answer);
}
private static void dfs(int x, int y, int length) {
if (!visited[x][y] && map[x][y] == 0) {
visited[x][y] = true;
map[x][y] = 1;
if (length == 3) {
int[][] virusTest = new int[n + 2][m + 2];
for (int i = 0; i <= n + 1; i++) {
for (int j = 0; j <= m + 1; j++) {
virusTest[i][j] = map[i][j];
}
}
Queue<Integer> xQueue = new LinkedList<>();
Queue<Integer> yQueue = new LinkedList<>();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (virusTest[i][j] == 2) {
xQueue.offer(i);
yQueue.offer(j);
}
}
}
while (!xQueue.isEmpty()) {
int a = xQueue.poll();
int b = yQueue.poll();
for (int i = 0; i < 4; i++) {
if (virusTest[a + xDir[i]][b + yDir[i]] == 0) {
virusTest[a + xDir[i]][b + yDir[i]] = 2;
xQueue.offer(a + xDir[i]);
yQueue.offer(b + yDir[i]);
}
}
}
int max = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (virusTest[i][j] == 0) {
max++;
}
}
}
answer = Math.max(answer, max);
}
else {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!visited[i][j] && map[i][j] == 0) {
dfs(i, j, length + 1);
}
}
}
}
}
if (visited[x][y]) {
visited[x][y] = false;
map[x][y] = 0;
}
}
}