Загрузка данных


public class LocalMax {
    public static void main(String[] args) {
        int[][] matrix = {
            {1, 9, 3},
            {4, 5, 8},
            {7, 2, 6}
        };

        int minLocalMax = Integer.MAX_VALUE;
        boolean found = false;

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {

                if (isLocalMax(matrix, i, j)) {
                    System.out.println("Локальный максимум: " + matrix[i][j] + " на позиции [" + i + "][" + j + "]");

                    if (matrix[i][j] < minLocalMax) {
                        minLocalMax = matrix[i][j];
                        found = true;
                    }
                }
            }
        }

        if (found) {
            System.out.println("\nНаименьший локальный максимум: " + minLocalMax);
        } else {
            System.out.println("Локальных максимумов нет");
        }
    }

    static boolean isLocalMax(int[][] matrix, int row, int col) {
        int value = matrix[row][col];

        // Проверяем всех соседей (вверх, вниз, влево, вправо)
        int[] dRow = {-1, 1, 0, 0};
        int[] dCol = { 0, 0,-1, 1};

        for (int d = 0; d < 4; d++) {
            int newRow = row + dRow[d];
            int newCol = col + dCol[d];

            // Если сосед существует и он >= текущего — не максимум
            if (newRow >= 0 && newRow < matrix.length &&
                newCol >= 0 && newCol < matrix[0].length) {

                if (matrix[newRow][newCol] >= value) {
                    return false;
                }
            }
        }

        return true;
    }
}