이 영역을 누르면 첫 페이지로 이동
시간의화살 블로그의 첫 페이지로 이동

시간의화살

페이지 맨 위로 올라가기

시간의화살

행복하세요

[백준] 2583 영역 구하기 - Java

  • 2022.07.29 16:05
  • Algorithm/Baekjoon

 

 

 

격자가 행렬로 구성되어있지 않고 수능 수학에서 많이 보던 x y격자로 구성되어있다.

 

이에 따라 입력받을 때 주의해야하고, 이거 말고는 그냥 bfs돌리면 된다.

 

 

 

import java.io.*;
import java.util.*;
 
public class Main {
    static int N, M, K;

    static int[][] map;
    static boolean[][] visit;    

    static int[] dr = {-1,1,0,0};
    static int[] dc = {0,0,-1,1};

    static ArrayList<Integer> list = new ArrayList<>();
    static int cnt;
    static int total_cnt;
    
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                
        
        StringTokenizer st = new StringTokenizer(br.readLine());

        M = Integer.parseInt(st.nextToken());
        N = Integer.parseInt(st.nextToken());
        K = Integer.parseInt(st.nextToken());

        map = new int[M][N];
        visit = new boolean[M][N];

        Square[] arr = new Square[K];

        for(int i=0; i<K; i++){
            st = new StringTokenizer(br.readLine());
            arr[i] = new Square(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
        }

        for(int i=0; i<K; i++){
            Square cur = arr[i];

            for(int j = cur.start_c; j<cur.fin_c  ; j++){
                for(int k = cur.start_r; k<cur.fin_r; k++){
                    map[j][k] = 1;
                }
            }

        }
        

        for(int i=0; i<M; i++){
            for(int j=0; j<N; j++){

                if(!visit[i][j] && map[i][j] == 0){
                    total_cnt++;
                    bfs(i,j);
                }   

            }
        }

        Collections.sort(list);

        System.out.println(total_cnt);
        for(int k : list){
            System.out.print(k + " ");
        }

        
        
        
    }

    static void bfs(int r, int c){
        cnt = 0;

        Queue<Locate> q = new LinkedList<>();
        q.add(new Locate(r,c));
        cnt++;
        visit[r][c] = true;

        while(!q.isEmpty()){
            Locate cur = q.poll();

            for(int i=0; i<4; i++){
                int nextR = cur.r + dr[i];
                int nextC = cur.c + dc[i];

                if(nextR >= M || nextR <= -1 || nextC >= N || nextC <= -1){
                    continue;
                }

                if(map[nextR][nextC] != 0){
                    continue;
                }

                if(visit[nextR][nextC]){
                    continue;
                }

                q.add(new Locate(nextR, nextC));
                visit[nextR][nextC] = true;
                cnt++;
            }
        }

        list.add(cnt);
        
        
    }
}

class Square{
    int start_r, fin_r, start_c, fin_c;

    Square(int start_r, int start_c, int fin_r, int fin_c){
        this.start_r = start_r;
        this.start_c = start_c;
        this.fin_r = fin_r;
        this.fin_c = fin_c;
    }
}

class Locate{
    int r,c;
    Locate(int r, int c){
        this.r = r;
        this.c = c;
    }
}
반응형

'Algorithm > Baekjoon' 카테고리의 다른 글

[백준] 10895 Great Pow! - Java  (0) 2022.08.05
[백준] 2146 다리 만들기 - Java  (0) 2022.07.31
[백준] 2468 안전 영역 - Java  (0) 2022.07.28
[백준] 25332 수들의 합 8 - Java  (0) 2022.07.28
[백준] 2015 수들의 합 4 - Java  (0) 2022.07.28

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • [백준] 10895 Great Pow! - Java

    [백준] 10895 Great Pow! - Java

    2022.08.05
  • [백준] 2146 다리 만들기 - Java

    [백준] 2146 다리 만들기 - Java

    2022.07.31
  • [백준] 2468 안전 영역 - Java

    [백준] 2468 안전 영역 - Java

    2022.07.28
  • [백준] 25332 수들의 합 8 - Java

    [백준] 25332 수들의 합 8 - Java

    2022.07.28
다른 글 더 둘러보기

정보

시간의화살 블로그의 첫 페이지로 이동

시간의화살

  • 시간의화살의 첫 페이지로 이동

검색

방문자

  • 전체 방문자
  • 오늘
  • 어제

카테고리

  • 분류 전체보기 (607)
    • Algorithm (205)
      • Data Structure (5)
      • Theory && Tip (33)
      • Baekjoon (166)
      • ALGOSPOT (1)
    • Spring (123)
      • Spring (28)
      • Spring Web MVC (20)
      • Spring Database (14)
      • Spring Boot (6)
      • Spring 3.1 (11)
      • Spring Batch (6)
      • Spring Security (16)
      • JPA (12)
      • Spring Data JPA (5)
      • QueryDSL (4)
      • eGovFramework (1)
    • Programming Language (74)
      • Java (19)
      • JavaScript (15)
      • C (25)
      • C++ (12)
      • Python (1)
      • PHP (2)
    • Computer Science (69)
      • Operating System (18)
      • Computer Network (17)
      • System Programming (22)
      • Universial Programming Lang.. (8)
      • Computer Architecture (4)
    • Database (21)
      • Database (7)
      • MySQL (3)
      • Oracle (3)
      • Redis (5)
      • Elasticsearch (3)
    • DevOps (20)
      • Docker && Kubernetes (8)
      • Jenkins (4)
      • Github Actions (0)
      • Amazon Web Service (8)
    • Machine Learning (28)
      • AI Introduction (28)
    • Mobile (28)
      • Android (21)
      • Flutter (7)
    • Solutions (14)
    • Life Logs (0)
    • 낙서장 (25)

최근 글

나의 외부 링크

메뉴

  • 홈

정보

13months의 시간의화살

시간의화살

13months

블로그 구독하기

  • 구독하기
  • RSS 피드

티스토리

  • 티스토리 홈
  • 이 블로그 관리하기
  • 글쓰기
Powered by Tistory / Kakao. Copyright © 13months.

티스토리툴바