public static int[] solution(String[] maps) {
boolean[][] visited = new boolean[maps.length][maps[0].length()];
List<Integer> territories = new ArrayList<>();
for (int i = 0; i < maps.length; i++) {
for (int j = 0; j < maps[i].length(); j++) {
int territorySize = getTerritorySize(i, j, visited, maps);
if (territorySize > 0) {
territories.add(territorySize);
}
}
}
Collections.sort(territories);
return territories.isEmpty() ? new int[] { -1 } : territories.stream().mapToInt(Integer::intValue).toArray();
}
private static int getTerritorySize(int row, int col, boolean[][] visited, String[] maps) {
if (row < 0 || col < 0 || row >= visited.length || col >= visited[0].length || visited[row][col]
|| maps[row].charAt(col) == 'X') {
return 0;
}
visited[row][col] = true;
int territorySize = maps[row].charAt(col) - '0';
int[] dr = { -1, 1, 0, 0 };
int[] dc = { 0, 0, -1, 1 };
for (int i = 0; i < 4; i++) {
int newRow = row + dr[i];
int newCol = col + dc[i];
territorySize += getTerritorySize(newRow, newCol, visited, maps);
}
return territorySize;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/154540