been_dev
been_archive
been_dev
전체 방문자
오늘
어제
  • 분류 전체보기 (34)
    • f-lab (3)
    • project (2)
    • solve (29)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 에프랩 1개월 후기
  • 자바
  • f-lab 1개월 후기
  • Downloading from external resources is disabled
  • 백준
  • 프로그래머스
  • 숫자의표현
  • 에프랩
  • jadencase만들기
  • MYSQL
  • 그리디
  • specify location
  • 코딩테스트
  • 에프랩 2개월 후기
  • Lombok
  • Eclipse
  • 해시
  • 완전탐색
  • f-lab
  • 실행창 작음
  • 큐
  • f-lab 2개월 후기
  • 버튼미노출
  • 탐욕법
  • 자바 백엔드
  • JWT
  • 이진변환반복하기
  • 문자열
  • 후기
  • 스택

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
been_dev

been_archive

solve

[백준 2667/실버1] 단지 번호 붙이기 (자바)

2024. 1. 9. 21:45

DFS 

 

1. visited 배열 사용하여 정석으로 풀이

import java.util.*;
import java.io.*;

class Main {
    final static int MAX = 25 + 10;
    static boolean[][] graph;
    static boolean[][] visited;
    static int countPerDanji;
    static int dirY[] = {1, -1, 0, 0};
    static int dirX[] = {0, 0, 1, -1}; // 두 배열 합쳐서 상하좌우
    
    static void dfs(int y, int x) {
        visited[y][x] = true;
        countPerDanji++;
        
        // 상하좌우 갈 수 있는지 확인
        for(int i=0; i<4; i++){
            int newY = y + dirY[i];
            int newX = x + dirX[i];
            if(!visited[newY][newX] && graph[newY][newX]){
                dfs(newY,newX);
            }
        }
    }
    
    public static void main(String[] args) throws IOException {
        // 0. 입력 및 초기화
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine()); // N * N 크기의 정사각형
             
        graph = new boolean[MAX][MAX];
        visited = new boolean[MAX][MAX];
        
        for(int i=1; i<=N; i++) {
            String s = br.readLine();
            for(int j=1; j<=N; j++) {
                graph[i][j] = s.charAt(j-1) == '1';
            }
        }
        
        // 1. (1,1) 부터 (N,N)까지 돌면서 dfs
        // 각 단지가 몇 개의 아파트로 구성되어 있는지 저장
        ArrayList<Integer> countList = new ArrayList<>();
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                if(graph[i][j] && !visited[i][j]) {
                    countPerDanji = 0;
                    dfs(i, j); // 그래프 탐색
                    countList.add(countPerDanji);
                }
            }
        }
        // 2. 출력
        System.out.println(countList.size()); // 총 단지 수
        Collections.sort(countList);
        for(int i=0 ; i<countList.size(); i++){
            System.out.println(countList.get(i));
        }
        
        br.close();
    }   
}

 

2.  graph 배열만 사용하여 풀이

import java.util.*;
import java.io.*;

// graph 배열만 활용 
class Main {
    final static int MAX = 25 + 10;
    static boolean[][] graph;
    static int countPerDanji;
    static int dirY[] = {1, -1, 0, 0};
    static int dirX[] = {0, 0, 1, -1}; // 두 배열 합쳐서 상하좌우
    
    static void dfs(int y, int x) {
        graph[y][x] = false;
        countPerDanji++;
        
        // 상하좌우 갈 수 있는지 확인
        for(int i=0; i<4; i++){
            int newY = y + dirY[i];
            int newX = x + dirX[i];
            if(graph[newY][newX]){
                dfs(newY,newX);
            }
        }
    }
    
    public static void main(String[] args) throws IOException {
        // 0. 입력 및 초기화
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine()); // N * N 크기의 정사각형
             
        graph = new boolean[MAX][MAX];
        
        for(int i=1; i<=N; i++) {
            String s = br.readLine();
            for(int j=1; j<=N; j++) {
                graph[i][j] = s.charAt(j-1) == '1';
            }
        }
        
        // 1. (1,1) 부터 (N,N)까지 돌면서 dfs
        // 각 단지가 몇 개의 아파트로 구성되어 있는지 저장
        ArrayList<Integer> countList = new ArrayList<>();
        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                if(graph[i][j]) {
                    countPerDanji = 0;
                    dfs(i, j); // 그래프 탐색
                    countList.add(countPerDanji);
                }
            }
        }
        // 2. 출력
        System.out.println(countList.size()); // 총 단지 수
        Collections.sort(countList);
        for(int i=0 ; i<countList.size(); i++){
            System.out.println(countList.get(i));
        }
        
        br.close();
    }   
}

 

참고 : 개발자로 취직하기 티스토리 및 유튜브

'solve' 카테고리의 다른 글

[프로그래머스] Lv2. 피로도 (자바)  (0) 2024.01.09
[백준 1018] 체스판 다시 칠하기 (자바)  (0) 2024.01.09
[백준 2606] 바이러스 (자바)  (1) 2024.01.09
두번째 코딩테스트 벼락치기(또다시..)  (0) 2024.01.08
코딩테스트 벼락치기(이게 맞나..)  (1) 2023.07.23
    'solve' 카테고리의 다른 글
    • [백준 1018] 체스판 다시 칠하기 (자바)
    • [백준 2606] 바이러스 (자바)
    • 두번째 코딩테스트 벼락치기(또다시..)
    • 코딩테스트 벼락치기(이게 맞나..)
    been_dev
    been_dev

    티스토리툴바