Algorithm/Programmers
programmers_카카오프렌즈 컬러링북_java
owoowo
2023. 5. 31. 19:23
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/1829
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
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;
}
}