Algorithm/๋ฐฑ์ค€

๋ฐฑ์ค€ 2667๋ฒˆ : ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ (Java)

skyey94 2022. 3. 22. 20:04

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

 

2667๋ฒˆ: ๋‹จ์ง€๋ฒˆํ˜ธ๋ถ™์ด๊ธฐ

<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ง€๋„๊ฐ€ ์žˆ๋‹ค. 1์€ ์ง‘์ด ์žˆ๋Š” ๊ณณ์„, 0์€ ์ง‘์ด ์—†๋Š” ๊ณณ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ด ์ง€๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์—ฐ๊ฒฐ๋œ ์ง‘์˜ ๋ชจ์ž„์ธ ๋‹จ์ง€๋ฅผ ์ •์˜ํ•˜๊ณ , ๋‹จ์ง€์— ๋ฒˆํ˜ธ๋ฅผ ๋ถ™์ด๋ ค ํ•œ๋‹ค. ์—ฌ

www.acmicpc.net


๐Ÿ“– ํ’€์ด ๊ณผ์ •

- ์žฌ๊ท€๋ฅผ ์ด์šฉํ•˜์—ฌ DFS ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. (BFS๋ฐฉ์‹์œผ๋กœ ํ’€์–ด๋„ ๋ฉ๋‹ˆ๋‹ค.)

- ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด์˜ ๊ฐ’์ด 1์ด๋ฉด์„œ, ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์„ ๊ธฐ์ค€์œผ๋กœ DFS๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

- ์ดํ›„, DFS ์žฌ๊ท€๋‚ด์—์„œ count ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” Node๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ count ๋ณ€์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  - Main ๋ฉ”์†Œ๋“œ์—์„œ dfs() ๋ฉ”์†Œ๋“œ์˜ ์‹œ์ž‘์  ์ด ๊ณง count ๋ณ€์ˆ˜์— ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐํ™”๋ฅผ 1๋กœ ํ•ฉ๋‹ˆ๋‹ค.
  
- ์–ด์ฐจํ”ผ, ์ด ๋‹จ์ง€์ˆ˜๋Š” List์˜ size()์™€ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ์ฒดํฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

- ๋งˆ์ง€๋ง‰์œผ๋กœ List์— ๋‹ด์€ ๊ฐ’๋“ค์„ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ป ์ฝ”๋“œ

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    static int n,count;
    static int[] dx = {-1,0,1,0};
    static int[] dy = {0,-1,0,1};
    static int[][] arr;
    static boolean[][] visited;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuffer sb = new StringBuffer();
        List<Integer> answerList = new ArrayList<>();

        n = Integer.parseInt(br.readLine());
        arr = new int[n][n];
        visited = new boolean[n][n];

        for(int i = 0; i< n; i++){
            String input = br.readLine();
            for(int j = 0; j< n; j++){
                arr[i][j] = Integer.parseInt(String.valueOf(input.charAt(j)));
            }
        }

        for(int i = 0; i< n; i++){
            for(int j = 0; j< n; j++){
                if(arr[i][j] == 1 && !visited[i][j]){
                    count = 1;
                    answerList.add(dfs(i,j));
                }
            }
        }
        Collections.sort(answerList);

        sb.append(answerList.size()).append("\n");
        for(Integer i : answerList){
            sb.append(i).append("\n");
        }

        bw.write(sb.toString());
        bw.flush();
        bw.close();
        br.close();
    }

    private static int dfs(int x, int y){
        visited[x][y] = true;

        for(int i = 0; i< 4; i++){
            int nextX = x + dx[i];
            int nextY = y + dy[i];

            if(nextX >= 0 && nextY >= 0 && nextX < n && nextY < n){
                if(!visited[nextX][nextY] && arr[nextX][nextY] == 1){
                    dfs(nextX,nextY);
                    count += 1;
                }
            }
        }
        return count;
    }
}