본문 바로가기
코딩테스트 준비/백준

[백준] 2468번 - 안전 영역 [Java]

by mwzz6 2025. 2. 3.

https://www.acmicpc.net/problem/2468

 

[백준] 2468번 - 안전 영역 [Java]
[백준] 2468번 - 안전 영역 [Java]
[백준] 2468번 - 안전 영역 [Java]


1.  아이디어

 

물의 높이를 점점 높이면서 BFS, DFS로 영역의 개수를 구하는 걸 반복하면 해결할 수 있다.


2. 문제풀이

 

물의 높이를 height로 하는 for문에서 반복적으로 탐색하도록 구현했다. 높이를 높일 때마다 방문 체크 배열과 영역의 수를 새롭게 만들어서 탐색하는 과정을 주어진 영역의 최대 높이까지 반복하는 방식으로 구현했다.


3. 코드

 

import java.io.*;
import java.util.*;

public class Main_BFS {

    private static final int[] dr = {-1, 0, 1, 0};
    private static final int[] dc = {0, 1, 0, -1};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int N = Integer.parseInt(br.readLine());
        int maxHeight = 0;

        int[][] map = new int[N][N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());

            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                maxHeight = Math.max(maxHeight, map[i][j]);
            }
        }

        int max = 1;
        for (int height = 2; height <= maxHeight; height++) {
            boolean[][] visited = new boolean[N][N];
            int cnt = 0;

            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    if (map[i][j] < height || visited[i][j]) continue;

                    bfs(i, j, height, N, map, visited);
                    cnt++;
                }
            }

            max = Math.max(max, cnt);
        }

        System.out.println(max);
    }

    private static void bfs(int r, int c, int height, int N, int[][] map, boolean[][] visited) {
        Queue<int[]> q = new ArrayDeque<>();
        q.add(new int[]{r, c});

        visited[r][c] = true;

        while (!q.isEmpty()) {
            int[] node = q.remove();

            for (int dir = 0; dir < 4; dir++) {
                int nr = node[0] + dr[dir];
                int nc = node[1] + dc[dir];

                if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;
                if (map[nr][nc] < height || visited[nr][nc]) continue;

                q.add(new int[]{nr, nc});
                visited[nr][nc] = true;
            }
        }
    }

}
import java.io.*;
import java.util.*;

public class Main_DFS {

    private static final int[] dr = {-1, 0, 1, 0};
    private static final int[] dc = {0, 1, 0, -1};

    private static int N;
    private static int[][] map;
    private static boolean[][] visited;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        N = Integer.parseInt(br.readLine());
        int maxHeight = 0;

        map = new int[N][N];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());

            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                maxHeight = Math.max(maxHeight, map[i][j]);
            }
        }

        int max = 1;
        for (int height = 2; height <= maxHeight; height++) {
            visited = new boolean[N][N];
            int cnt = 0;

            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    if (map[i][j] < height || visited[i][j]) continue;

                    dfs(i, j, height);
                    cnt++;
                }
            }

            max = Math.max(max, cnt);
        }

        System.out.println(max);
    }

    private static void dfs(int r, int c, int height) {
        visited[r][c] = true;

        for (int dir = 0; dir < 4; dir++) {
            int nr = r + dr[dir];
            int nc = c + dc[dir];

            if (nr < 0 || nr >= N || nc < 0 || nc >= N) continue;
            if (map[nr][nc] < height || visited[nr][nc]) continue;

            dfs(nr, nc, height);
        }
    }

}

4. 후기

 

- BFS

- DFS