C++ 방식으로 푼 안전영역




DFS 방식

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static int[][] map;
    public static int[][] check;
    public static int N;

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

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        map = new int[N][N];
        check = new int[N][N];
        int maxday = 0;
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (maxday < map[i][j])
                    maxday = map[i][j];
            }
        } // INIT END
        int ans = 0, max = 0;
        for (int n = 0; n <= maxday; n++) {
            ans = sol(n);
            if (max < ans) {
                max = ans;
            }
        }
        System.out.println(max);

        br.close();
    }// Main End

    public static int sol(int day) {
        check = new int[N][N];
        int cnt = 0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (map[i][j] > day && check[i][j] == 0) {
                    check[i][j] = ++cnt;
                    dfs(day, i, j);
                }
            }
        }
        return cnt;
    }

    public static void dfs(int day, int r, int c) {
        for (int d = 0; d < dr.length; d++) {
            int nr = r + dr[d];
            int nc = c + dc[d];
            if (isIn(nr, nc) && map[nr][nc] > day && check[nr][nc] == 0) {
                check[nr][nc] = check[r][c];
                dfs(day, nr, nc);
            }
        }
    }

    public static boolean isIn(int r, int c) {
        return (0 <= r && r < N && 0 <= c && c < N);
    }
}



BFS방식

import java.awt.Point;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
    public static int[][] map;
    public static int[][] check;
    public static int N;
    public static Queue<Point> q = new LinkedList<Point>();;
    public static int[] dr = { 1, 0, -1, 0 };
    public static int[] dc = { 0, 1, 0, -1 };

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

        N = Integer.parseInt(br.readLine());
        map = new int[N][N];
        int maxday = 0;
        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < N; j++) {
                map[i][j] = Integer.parseInt(st.nextToken());
                if (maxday < map[i][j])
                    maxday = map[i][j];
            }
        } // INIT END
        int max = 0;
        for (int n = 0; n <= maxday; n++) {
            check = new int[N][N];
            int ans = sol(n);
            if (max < ans) {
                max = ans;
            }

        }

        System.out.println(max);

        br.close();
    }// Main End

    public static int sol(int day) {
        int cnt = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (map[i][j] > day && check[i][j] == 0) {
                    check[i][j] = ++cnt;
                    q.offer(new Point(i, j));
                }
                while (!q.isEmpty()) {
                    int r = q.peek().x;
                    int c = q.poll().y;

                    for (int d = 0; d < dr.length; d++) {
                        int nr = r + dr[d];
                        int nc = c + dc[d];

                        if (isIn(nr, nc) && map[nr][nc] > day && check[nr][nc] == 0) {
                            check[nr][nc] = check[r][c];
                            q.offer(new Point(nr, nc));
                        }
                    }

                } // END WHILE LOOP

            }
        }

        return cnt;
    }

    public static boolean isIn(int r, int c) {
        return (0 <= r && r < N && 0 <= c && c < N);
    }
}

왈독님의 창작활동을 응원하고 싶으세요?