Skip to content

Commit f3c4cbe

Browse files
authored
[20260201] BOJ / G3 / 성곽 / 한종욱
1 parent c3ab214 commit f3c4cbe

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

Ukj0ng/202602/01 BOJ G3 성곽.md

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static final int[] dx = {0, -1, 0, 1};
9+
private static final int[] dy = {-1, 0, 1, 0};
10+
private static Map<Integer, Integer> rooms;
11+
private static Set<Integer>[] near;
12+
private static int[][] arr;
13+
private static boolean[][] visited;
14+
private static boolean[][][] wall;
15+
private static int N, M, answer1, answer2, answer3;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
20+
int num = 1;
21+
22+
for (int i = 0; i < M; i++) {
23+
for (int j = 0; j < N; j++) {
24+
if (!visited[i][j]) {
25+
BFS(i, j, num++);
26+
}
27+
}
28+
}
29+
30+
near = new Set[rooms.size()+1];
31+
32+
for (int i = 1; i <= rooms.size(); i++) {
33+
near[i] = new HashSet<>();
34+
}
35+
36+
for (int i = 0; i < M; i++) {
37+
Arrays.fill(visited[i], false);
38+
}
39+
40+
for (int i = 0; i < M; i++) {
41+
for (int j = 0; j < N; j++) {
42+
if (!visited[i][j]) {
43+
setNear(i, j, arr[i][j]);
44+
}
45+
}
46+
}
47+
48+
answer1 = rooms.size();
49+
for (int key : rooms.keySet()) {
50+
answer2 = Math.max(answer2, rooms.get(key));
51+
}
52+
53+
for (int i = 1; i <= answer1; i++) {
54+
for (int e : near[i]) {
55+
answer3 = Math.max(answer3, rooms.get(i)+rooms.get(e));
56+
}
57+
}
58+
59+
bw.write(answer1 + "\n" + answer2 + "\n" + answer3);
60+
bw.flush();
61+
bw.close();
62+
br.close();
63+
}
64+
65+
private static void init() throws IOException {
66+
StringTokenizer st = new StringTokenizer(br.readLine());
67+
N = Integer.parseInt(st.nextToken());
68+
M = Integer.parseInt(st.nextToken());
69+
70+
arr = new int[M][N];
71+
visited = new boolean[M][N];
72+
wall = new boolean[M][N][4];
73+
74+
rooms = new HashMap<>();
75+
76+
for (int i = 0; i < M; i++) {
77+
st = new StringTokenizer(br.readLine());
78+
for (int j = 0; j < N; j++) {
79+
int temp = Integer.parseInt(st.nextToken());
80+
81+
if (temp >= 8) {
82+
temp -= 8;
83+
wall[i][j][3] = true;
84+
}
85+
86+
if (temp >= 4) {
87+
temp -= 4;
88+
wall[i][j][2] = true;
89+
}
90+
91+
if (temp >= 2) {
92+
temp -= 2;
93+
wall[i][j][1] = true;
94+
}
95+
96+
if (temp >= 1) {
97+
temp -= 1;
98+
wall[i][j][0] = true;
99+
}
100+
}
101+
}
102+
}
103+
104+
private static void BFS(int x, int y, int index) {
105+
Queue<int[]> q = new ArrayDeque<>();
106+
int count = 1;
107+
visited[x][y] = true;
108+
arr[x][y] = index;
109+
q.add(new int[] {x, y, index});
110+
111+
while (!q.isEmpty()) {
112+
int[] current = q.poll();
113+
114+
for (int i = 0; i < 4; i++) {
115+
if (wall[current[0]][current[1]][i]) continue;
116+
int nx = current[0] + dx[i];
117+
int ny = current[1] + dy[i];
118+
119+
if (OOB(nx, ny) || visited[nx][ny]) continue;
120+
visited[nx][ny] = true;
121+
count++;
122+
arr[nx][ny] = index;
123+
q.add(new int[] {nx, ny, index});
124+
}
125+
}
126+
127+
rooms.put(index, count);
128+
}
129+
130+
private static void setNear(int x, int y, int index) {
131+
Queue<int[]> q = new ArrayDeque<>();
132+
visited[x][y] = true;
133+
q.add(new int[] {x, y});
134+
135+
while (!q.isEmpty()) {
136+
int[] current = q.poll();
137+
138+
for (int i = 0; i < 4; i++) {
139+
int nx = current[0] + dx[i];
140+
int ny = current[1] + dy[i];
141+
142+
if (OOB(nx, ny) || visited[nx][ny]) continue;
143+
if (arr[current[0]][current[1]] != arr[nx][ny]) {
144+
near[index].add(arr[nx][ny]);
145+
continue;
146+
}
147+
visited[nx][ny] = true;
148+
q.add(new int[] {nx, ny});
149+
}
150+
}
151+
}
152+
153+
private static boolean OOB(int nx, int ny) {
154+
return nx < 0 || nx > M-1 || ny < 0 || ny > N-1;
155+
}
156+
}
157+
```

0 commit comments

Comments
 (0)