팡세영
Log sey
팡세영
전체 방문자
오늘
어제
  • 분류 전체보기 (74)
    • PS (45)
      • programmers (13)
      • 백준 (29)
    • Android (16)
    • Daily (0)
    • Kotlin (6)
    • Design Pattern (2)
    • Java (1)
    • Flutter (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 안드로이드
  • programmers #프로그래머스
  • compose
  • 해쉬맵
  • Kotlin
  • LEVEL2
  • mvvm
  • DFS
  • ArcitecturePattern
  • 정렬
  • 실버
  • 의존성 주입
  • 코틀린
  • 백준
  • Android
  • flutter
  • TestCode
  • 하단네비게이션바
  • binding
  • 이분탐색
  • 구현
  • BFS
  • 문자열
  • 완전탐색
  • 자바
  • 프로그래머스
  • 골드
  • CustomView
  • java
  • programmers

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
팡세영

Log sey

프로그래머스 카카오 프렌즈 컬러링 북
PS/programmers

프로그래머스 카카오 프렌즈 컬러링 북

2022. 6. 27. 02:04

프로그래머스 카카오 프렌즈 컬러링 북

풀이

  • bfs를 이용하여 해결
  • 소요 시간 약 20분

class Pos{
    public int x;
    public int y;

    public Pos(int y, int x){
        this.y = y;
        this.x = x;
    }

}

class Solution {
    int dx[] = {0, 0, -1, 1};   // 상 하 좌 우
    int dy[] = {-1, 1, 0, 0};
    boolean visited[][];
    PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());

    public int[] solution(int m, int n, int[][] picture) {
        int numberOfArea = 0;
        int maxSizeOfOneArea = 0;
        visited = new boolean[m][n];

        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(visited[i][j] == false && picture[i][j] != 0){
                    numberOfArea++;
                    bfs(i, j, picture[i][j], picture);
                }
            }
        }

        maxSizeOfOneArea = pq.poll();

        int[] answer = new int[2];
        answer[0] = numberOfArea;
        answer[1] = maxSizeOfOneArea;
        return answer;
    }

    public void bfs(int y, int x, int sig, int [][] picture){
        Queue<Pos> queue = new LinkedList<>();
        queue.add(new Pos(y,x));
        visited[y][x] = true;

        int len = 1;

        while(!queue.isEmpty()){
            Pos tmp = queue.poll();
            int curX = tmp.x;
            int curY = tmp.y;

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

                if(nextX >= 0 && nextX < picture[0].length && nextY >= 0 && nextY < picture.length){
                    if(picture[nextY][nextX] == sig && visited[nextY][nextX] == false){
                        visited[nextY][nextX] = true;
                        len++;
                        queue.add(new Pos(nextY,nextX));
                    }
                }
            }
        }
        pq.add(len);

    }
}

'PS > programmers' 카테고리의 다른 글

프로그래머스 타겟 넘버  (0) 2022.06.27
프로그래머스 두 개 뽑아서 더하기  (0) 2022.06.27
프로그래머스 실패율  (0) 2022.06.27
프로그래머스 1차 다트 게임  (0) 2022.06.27
프로그래머스 게임 맵 최단 거리  (0) 2022.06.27
    'PS/programmers' 카테고리의 다른 글
    • 프로그래머스 두 개 뽑아서 더하기
    • 프로그래머스 실패율
    • 프로그래머스 1차 다트 게임
    • 프로그래머스 게임 맵 최단 거리
    팡세영
    팡세영
    Android, CS, PS

    티스토리툴바