package task;
public class Main {
public static void main(String[] args) {
int[][] matrix = {
{5, 4, 5, 6, 7},
{3, 2, 3, 4, 5},
{4, 3, 1, 3, 6},
{7, 2, 4, 5, 8},
{8, 6, 7, 9, 10}
};
int result = countLocalMinima(matrix);
System.out.println("Количество локальных минимумов: " + result);
}
public static int countLocalMinima(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int rows = matrix.length;
int cols = matrix[0].length;
int count = 0;
int[] dx = {-1, -1, -1, 0, 0, 1, 1, 1};
int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
boolean isLocalMin = true;
int currentValue = matrix[i][j];
for (int dir = 0; dir < 8; dir++) {
int ni = i + dx[dir];
int nj = j + dy[dir];
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols) {
if (matrix[ni][nj] <= currentValue) {
isLocalMin = false;
break;
}
}
}
if (isLocalMin) {
count++;
}
}
}
return count;
}
}