csct3434
[level 2] 카카오프렌즈 컬러링북 - 1829 본문
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
class Solution {
private static final int[] dx = {0, 0, 1, -1};
private static final int[] dy = {1, -1, 0, 0};
private boolean[][] visited = new boolean[100][100];
private int sizeOfArea;
public int[] solution(int m, int n, int[][] picture) {
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && picture[i][j] != 0) {
numberOfArea++;
sizeOfArea = 0;
dfs(i, j, m, n, picture);
maxSizeOfOneArea = Math.max(maxSizeOfOneArea, sizeOfArea);
}
}
}
int[] answer = new int[2];
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
private void dfs(int x, int y, final int m, final int n, final int[][] picture) {
visited[x][y] = true;
sizeOfArea++;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (canGo(nx, ny, m, n, picture, picture[x][y])) {
dfs(nx, ny, m, n, picture);
}
}
}
private boolean canGo(int x, int y, int m, int n, int[][] picture, int color) {
if(!(x >= 0 && x < m && y >= 0 && y < n)) {
return false;
}
return !visited[x][y] && picture[x][y] == color;
}
}
'프로그래머스' 카테고리의 다른 글
[level 2] 3 x n 타일링 - 12902 (0) | 2024.02.28 |
---|---|
[level 2] 2 x n 타일링 - 12900 (0) | 2024.02.28 |
[level 2] 124 나라의 숫자 - 12899 (0) | 2024.02.28 |
[level 2] 게임 맵 최단거리 - 1844 (0) | 2024.02.28 |
[level 2] 단체사진 찍기 - 1835 (0) | 2024.02.28 |