ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 4-6: 단지번호 붙이기
    Recursion 2018. 10. 24. 20:17

    단지번호 붙이기 (danji.cpp)

     

    문제


    <그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집들의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여기서 연결되었다는 것은 어떤 집이 좌우, 혹은 아래위로 다른 집이 있는 경우를 말한다. 대각선상에 집이 있는 경우는 연결된 것이 아니다. <그림 2>는 <그림 1>을 단지별로 번호를 붙인 것이다. 지도를 입력하여 단지수를 출력하고, 각 단지에 속하는 집의 수를 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오.

    tetrismapex

     

    입력


    첫 번째 줄에는 지도의 크기 N(정사각형이므로 가로와 세로의 크기는 같으며 5≤N≤25)이 입력되고, 그 다음 N줄에는 각각 N개의 자료(0혹은 1)가 입력된다. (숫자와 숫자사이에는 공백이 없다.)

     

    출력


    첫 번째 줄에는 총 단지수를 출력하시오. 그리고 각 단지내 집의 수를 오름차순으로 정렬하여 한 줄에 하나씩 출력하시오.

     

    예제 입력

    7
    0110100
    0110101
    1110101
    0000111
    0100000
    0111110
    0111000

    예제 출력

    3
    7
    8
    9


    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Scanner;

    public class danji {

    /**
    *
    * 문제:
    * <그림 1>과 같이 정사각형 모양의 지도가 있다.
    * 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집들의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다.
    * 여기서 연결되었다는 것은 어떤 집이 좌우, 혹은 아래위로 다른 집이 있는 경우를 말한다. 대각선상에 집이 있는 경우는 연결된 것이 아니다.
    * <그림 2><그림 1>을 단지별로 번호를 붙인 것이다. 지도를 입력하여 단지수를 출력하고, 각 단지에 속하는 집의 수를 오름차순으로 정렬하여 출력하는 프로그램을 작성하시오.
    *
    *
    * 입력:
    * 첫 번째 줄에는 지도의 크기 N(정사각형이므로 가로와 세로의 크기는 같으며 5≤N≤25)이 입력되고, 그 다음 N줄에는 각각 N개의 자료(0혹은 1)가 입력된다. (숫자와 숫자사이에는 공백이 없다.)
    *
    * 출력:
    * 첫 번째 줄에는 총 단지수를 출력하시오. 그리고 각 단지내 집의 수를 오름차순으로 정렬하여 한 줄에 하나씩 출력하시오.
    *
    * 예제 입력:
    * 7
    * 0110100
    * 0110101
    * 1110101
    * 0000111
    * 0100000
    * 0111110
    * 0111000
    *
    * 예제 출력:
    * 3
    * 7
    * 8
    * 9
    *
    * @param args
    */

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    sc.nextLine();
    int combinations[][] = new int[N][N];
    boolean visited[][] = new boolean[N][N];
    ArrayList<Integer> danjiCountList = new ArrayList<>();
    int[] count = new int[1];

    for (int i=0;i<N; i++){
    String line = sc.nextLine();
    // System.out.println("line: " + line);
    for (int j=0; j<N; j++){
    combinations[i][j] = Integer.parseInt(String.valueOf(line.charAt(j)));
    }
    }

    for (int i=0;i<N; i++){
    for (int j=0; j<N; j++){
    if (combinations[i][j] == 1 && !visited[i][j]){
    countCurrentDanji(count, combinations, visited, i, j);
    // insertSorted(danjiCountList, count[0]);
    danjiCountList.add(count[0]);
    count[0] = 0;
    }
    }
    }

    Collections.sort(danjiCountList);

    System.out.println(danjiCountList.size());
    for (Integer danjiCount : danjiCountList){
    System.out.println(danjiCount);
    }

    }

    private static void insertSorted(ArrayList<Integer> danjiCountList, int count) {
    //insert count value at correctly sorted position
    if (danjiCountList.size() == 0) {
    danjiCountList.add(count);
    } else {
    boolean isAdded= false;
    for (int i=0 ;i<danjiCountList.size(); i++){
    if (danjiCountList.get(i) > count){
    //place here, and shift rest
    danjiCountList.add(i, count);
    isAdded = true;
    }
    }
    if (!isAdded) danjiCountList.add(count);
    }
    }

    private static void countCurrentDanji(int[] count, int[][] combinations, boolean[][] visited, int row, int col) {

    // System.out.println("printDanji row: " + row + ", col: " + col);
    if (row >= visited.length || col >= visited.length || col<0 || row<0) {
    return;
    }

    if (visited[row][col] || combinations[row][col] == 0) return;

    count[0] ++;
    visited[row][col] = true;

    //up
    countCurrentDanji(count, combinations, visited, row-1, col);

    //down
    countCurrentDanji(count, combinations, visited, row+1, col);

    //left
    countCurrentDanji(count, combinations, visited, row, col-1);

    //right
    countCurrentDanji(count, combinations, visited, row, col+1);

    }
    }


    'Recursion' 카테고리의 다른 글

    4-8: 부등호  (0) 2018.10.24
    4-7: 좋은 수열  (0) 2018.10.24
    4-5: Dessert  (0) 2018.10.24
    4-4: Division  (0) 2018.10.24
    4-3: k개의 1을 가진 n자리 이진패턴 경우 출력  (0) 2018.10.24
Designed by Tistory.