728x90
package prog;
import java.util.Arrays;
import java.util.LinkedList;
public class 카카오_프렌즈_컬러링북 {
public static void main(String[] args) {
Solution_카카오_프렌즈_컬러링북 s = new Solution_카카오_프렌즈_컬러링북();
System.out.println(Arrays.toString(s.solution( 6,4,new int[][] {{1, 1, 1, 0}, {1, 2, 2, 0}, {1, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 3}, {0, 0, 0, 3}})));
System.out.println(Arrays.toString(s.solution( 6,4,new int[][] {{1, 1, 1, 0}, {1, 1, 1, 0}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}, {0, 0, 0, 1}})));
}
}
class Solution_카카오_프렌즈_컬러링북 {
static int[][] dMove={{-1,0},{0,1},{1,0},{0,-1}};
int N;
int M;
public int[] solution(int n, int m, int[][] picture) {
int numberOfArea = 0;
int maxSizeOfOneArea = 0;
int[] answer = new int[2];
N=n;
M=m;
boolean[][] check = new boolean[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(picture[i][j]!=0 && !check[i][j]){
maxSizeOfOneArea = Math.max(bfs(picture,i,j,check),maxSizeOfOneArea);
numberOfArea++;
}
}
}
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
private int bfs(int[][] picture, int x, int y, boolean[][] check) {
LinkedList<Pos> q = new LinkedList<>();
int color = picture[x][y];
q.add(new Pos(x,y,color));
check[x][y]=true;
int cnt=1;
while(!q.isEmpty()){
Pos now = q.poll();
for (int i = 0; i < 4; i++) {
int nx=now.x+dMove[i][0];
int ny=now.y+dMove[i][1];
if(nx>=0 && nx<N && ny>=0 &&ny<M &&picture[nx][ny]==color && !check[nx][ny])
{
q.add(new Pos(nx,ny,color));
check[nx][ny]=true;
cnt++;
}
}
}
return cnt;
}
}
class Pos{
int x;
int y;
int color;
public Pos(int x, int y,int color) {
this.x = x;
this.y = y;
this.color=color;
}
}
728x90