package com.company;
import java.util.Arrays;
public class Solution {
static public void main(String[] args) {
int m = 6;
int n = 4;
int[][] picture = {{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(solution(m, n, picture)));
}
static int numberOfArea;
static int maxSizeOfOneArea;
static int pictureHeight;
static int pictureWidth;
static int[][] pictureGraph;
static int standardValue;
static int tempWidthValue;
static public int[] solution(int height, int width, int[][] picture) {
numberOfArea = 0;
maxSizeOfOneArea = 0;
pictureGraph = new int[picture.length][picture[0].length];
for (int i = 0; i < picture.length; i++) {
System.arraycopy(picture[i], 0, pictureGraph[i], 0, picture[i].length);
}
System.out.println(Arrays.deepToString(pictureGraph));
pictureHeight = height;
pictureWidth = width;
tempWidthValue = 0;
int[] answer = new int[2];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if(pictureGraph[y][x] != 0 ){
numberOfArea++;
tempWidthValue = 0;
standardValue = pictureGraph[y][x];
dfs(y, x);
maxSizeOfOneArea = Math.max(maxSizeOfOneArea, tempWidthValue);
}
}
}
answer[0] = numberOfArea;
answer[1] = maxSizeOfOneArea;
return answer;
}
private static void dfs(int y, int x) {
if (y < 0 || x < 0 || x >= pictureWidth || y >= pictureHeight || pictureGraph[y][x] == 0) {
return;
}
if(pictureGraph[y][x] == standardValue){
tempWidthValue++;
}else{
return;
}
pictureGraph[y][x] = 0;
dfs(y-1, x);
dfs(y, x+1);
dfs(y+1, x);
dfs(y, x-1);
}
}