코딩 공부 연습

백준 2667 단지번호붙이기

miffy짱 2022. 12. 7. 01:33
반응형

오늘은 단지번호붙이기 문제를 풀었다.

여전히 BFS 문제를 풀고있는데,

다 아무 문제없이 이제 해결하는거 같은데 크기가 하나인, 즉 처음한번 큐에 들어가고 바로 큐가 비고 끝나는 사이즈 하나 짜리일 때는  사이즈가 계속 틀리게 나오는데, 그 이유가 궁금해서 한번 알아보려고 한다. 예외로 처리하는것 말곤 답이없나?

 

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Queue<Pair> q = new LinkedList();
        Scanner scanner = new Scanner(System.in);
        int n;
        int table[][] = new int[26][26];
        int visited[][] = new int[26][26];
        List<Integer> dir_X = List.of(1, 0, -1, 0);
        List<Integer> dir_y = List.of(0, 1, 0, -1);

        n = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < n; i++) {
            String s = scanner.nextLine();
            for (int j = 0; j < n; j++) {
                table[i][j] = Integer.parseInt(String.valueOf(s.charAt(j)));
            }
        }
        List<Integer> danji = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (table[i][j] != 0 && visited[i][j] != 1) {
                    //dfs START
                    int cnt = 0;
                    q.add(new Pair(i,j));

                    while (!q.isEmpty()) {
                        Pair cur = q.peek();
                        q.remove();

                        for (int dir = 0; dir < 4; dir++) {
                            int c_x = cur.x + dir_X.get(dir);
                            int c_y = cur.y + dir_y.get(dir);
                            if (c_x < 0 || c_y < 0 || c_x >= n || c_y >= n) continue;
                            if (table[c_x][c_y] != 1 || visited[c_x][c_y] == 1) continue;
                            visited[c_x][c_y] = 1;
                            q.add(new Pair(c_x, c_y));
                            cnt++;
                        }
                    }
                    danji.add(cnt);
                }
            }
        }

        System.out.println(danji.size());
        Collections.sort(danji);
        for (int i = 0; i < danji.size(); i++) {
            if (danji.get(i) == 0) {
                System.out.println(1);
                continue;
            }
            System.out.println(danji.get(i));
        }
    }

    public static class Pair {
        int x,y;
        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }


}

보면 메인 맨밑에 출력할 때 사이즈가 0이라 나온 경우만 1로 출력해주는걸 볼 수 있다. 이걸 해주어야만 정답처리가 된다. 허허