๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Problem Solving

[Java] ๋ฐฑ์ค€ 1724 - ๊ทธ๋ฆผํŒ

๋ฌธ์ œ

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();
	}
}