๋ฌธ์
https://www.acmicpc.net/problem/1724
๋ฌธ์ ์ ๋์์์ง๋ ์์ง๋ง Sx = Ex์ด๊ฑฐ๋ Sy = Ey๋ผ๋ ์กฐ๊ฑด์ด ์๋ค๋ ๊ฒ ๊ฐ๋ค. (= ๋๊ฐ์ ์ ์๋ค๋ ๋ป)
ํ์ด
๋๊ฐ์ ์ด ์๋ค๋ ๊ฐ์ ํ์๋ ๊ตฌ์ญ๋ง ์ ๋๋ ์ฃผ๊ณ ๊ฐ ๊ตฌ์ญ๋ง๋ค Flood Fill (BFS)๋ง ๋๋ ค์ฃผ๋ฉด ๋๋ค.
๊ตฌ์ญ์ ๋๋๋๋ ์ ์ ์นธ์ผ๋ก ์๊ฐํ๋ฉด ์ฝ๋ค. ์๋ฅผ ๋ค์ด ์์ ์ ๋ ฅ 1 ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฐ์ด์ ๋ง๋ค์ด์ ํ์ฉํ๋ฉด ๋๋ค. ๋ฌผ๋ก ์ด๋ ๊ฒ ํ๋ฉด ๋ฐฐ์ด์ ํฌํค๊ฐ 4๋ฐฐ๊ฐ ๋์ด์ ์๊ฐ/๋ฉ๋ชจ๋ฆฌ ํจ์จ์ฑ์ ์กฐ๊ธ ๋จ์ด์ง๊ฒ ์ง๋ง, ์ด์ฐจํผ N, M ๊ฐ์ด ์๊ณ ์ ์ฒด์ ์ธ ๊ตฌํ์ด ๋จ์ํด์ ธ์ ์ข๋ค.
์ฝ๋
๋๋ณด๊ธฐ
import java.io.*;
import java.util.*;
public class Main {
public static class Pos {
public int row, col;
public Pos(int r, int c) {
row = r;
col = c;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int R = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
boolean[][] board = new boolean[2 * R + 1][2 * C + 1]; // true = ์ ๋ถ
int T = Integer.parseInt(br.readLine());
while (T-- > 0) {
st = new StringTokenizer(br.readLine());
int startR = Integer.parseInt(st.nextToken());
int startC = Integer.parseInt(st.nextToken());
int endR = Integer.parseInt(st.nextToken());
int endC = Integer.parseInt(st.nextToken());
if (startR == endR) {
int minC = Math.min(startC, endC);
int maxC = Math.max(startC, endC);
for (int c = minC * 2; c <= maxC * 2; c++) {
board[startR * 2][c] = true;
}
} else if (startC == endC) {
int minR = Math.min(startR, endR);
int maxR = Math.max(startR, endR);
for (int r = minR * 2; r <= maxR * 2; r++) {
board[r][startC * 2] = true;
}
}
}
boolean[][] visited = new boolean[2 * R + 1][2 * C + 1];
int maxSize = 0;
int minSize = R * C;
for (int r = 0; r <= R * 2; r++) {
for (int c = 0; c <= C * 2; c++) {
if (!visited[r][c] && !board[r][c]) {
visited[r][c] = true;
int size = 0;
Queue<Pos> que = new LinkedList<>();
int[] dirR = { -1, 1, 0, 0 };
int[] dirC = { 0, 0, -1, 1 };
que.add(new Pos(r, c));
while (!que.isEmpty()) {
Pos cur = que.remove();
if (cur.row % 2 == 1 && cur.col % 2 == 1) {
size++;
}
for (int d = 0; d < 4; d++) {
int row = cur.row + dirR[d];
int col = cur.col + dirC[d];
if (row < 0 || row > R * 2 || col < 0 || col > C * 2) {
continue;
}
if (visited[row][col] || board[row][col]) {
continue;
}
visited[row][col] = true;
que.add(new Pos(row, col));
}
}
maxSize = Math.max(maxSize, size);
minSize = Math.min(minSize, size);
}
}
}
System.out.println(maxSize);
System.out.println(minSize);
br.close();
}
}
'Problem Solving' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๋ฐฑ์ค 31443 - ์ค์์ด (1) | 2024.03.15 |
---|---|
[Java] ๋ฐฑ์ค 2252 - ์ค ์ธ์ฐ๊ธฐ (0) | 2024.03.14 |
[Java] ๋ฐฑ์ค 1999 - ์ต๋์ต์ (0) | 2024.03.01 |
[Java] ๋ฐฑ์ค 16946 - ๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ 4 (0) | 2024.02.29 |
[Java] ๋ฐฑ์ค 31091 - ๊ฑฐ์ง๋ง (0) | 2024.02.23 |