programmers_카카오프렌즈 컬러링북_java
2023. 5. 31. 19:23ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/1829
import java.util.*;
class Solution {
public static int[][] delta = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; //상 하 좌 우
public int[] solution(int m, int n, int[][] picture) {
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i<m; i++){
for (int j = 0; j<n; j++){
if (!visited[i][j] && picture[i][j] != 0){
numberOfArea++;
maxSizeOfOneArea = Math.max(maxSizeOfOneArea, findArea(m, n, picture, i, j, visited));
}
}
}
int[] answer = new int[2];
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
public int findArea(int m, int n, int[][] picture, int y, int x, boolean[][] visited){
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{y, x});
int cnt = 0;
while (!queue.isEmpty()){
int[] now = queue.poll();
if (visited[now[0]][now[1]]) continue;
visited[now[0]][now[1]] = true;
cnt++;
for (int i = 0; i< 4; i++){
int nexty = now[0] + delta[i][0];
int nextx = now[1] + delta[i][1];
if (0<= nexty && nexty < m
&& 0<= nextx && nextx < n
&& !visited[nexty][nextx]
&& picture[nexty][nextx] == picture[now[0]][now[1]]
){
queue.offer(new int[]{nexty, nextx});
}
}
}
return cnt;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_택배 배달과 수거하기_java (0) | 2023.06.01 |
---|---|
programmers_교점에 별 만들기_java (0) | 2023.06.01 |
programmers_양궁대회_java (0) | 2023.05.29 |
programmers_과제 진행하기_java (1) | 2023.05.26 |
programmers_요격 시스템_java (0) | 2023.05.25 |