알고리즘/baekjoon

[ baekjoon ] 단지번호붙이기 2667번 ( Java)

Yanoo 2021. 11. 30. 21:48
728x90
반응형

문제

백준 2667 단지번호붙이기 풀이 ( 자바 )

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

 

2667번: 단지번호붙이기

<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여

www.acmicpc.net

 

풀이

간단한 dfs문제였다.

package baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class _2667 {
    public static int[][] graph;

    public static int n;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());

        graph = new int[n][n];

        ArrayList<Integer> answer = new ArrayList<>();

        for(int i = 0; i < n; i++) {
            String tmp = br.readLine();
            for (int j = 0; j < n; j++) {
                graph[i][j] = Integer.parseInt(tmp.charAt(j) + "");
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (graph[i][j] == 1) {
                    int cnt = dfs(i, j);
                    answer.add(cnt);
                }
            }
        }

        Collections.sort(answer);

        System.out.println(answer.size());
        for (int i = 0; i < answer.size(); i++) {
            System.out.println(answer.get(i));
        }
    }

    public static int dfs(int x, int y) {
        if (x < 0 || y < 0 || x >= n || y >= n) {
            return 0;
        }
        if (graph[x][y] == 1) {
            graph[x][y] = 0;
            int cnt = 0;
            cnt += 1;
            cnt += dfs(x + 1, y);
            cnt += dfs(x, y + 1);
            cnt += dfs(x - 1, y);
            cnt += dfs(x, y - 1);

            return cnt;
        }

        return 0;
    }
}
728x90
반응형