Local Minimum Cell | ISC Computer Science 2027 Practical

Each element of a matrix denotes the elevation of a platform. A platform is classified as a Local Minimum Cell if its elevation is strictly lower than the elevations of its four adjacent platforms in the cardinal directions (up, down, left and right). Only interior cells of the matrix are eligible for consideration, as boundary cells do not possess four immediate neighbors.

Write a program to declare a matrix H[][] of order (M × N) where 4 ≤ M ≤ 9 and 4 ≤ N ≤ 9 (check for validity). Accept integer heights and perform the following tasks:
a. Display the matrix.
b. Display all Local Minimum Cell and their positions.
c. Find and display the Local Minimum Cell (LMC) which is the smallest.

Example 1
INPUT:
M = 4
N = 4
Enter the numbers:
8 7 9 6 6 1 8 5 9 7 2 8 5 6 7 9
OUTPUT:
THE MATRIX IS:
8 7 9 6
6 1 8 5
9 7 2 8
5 6 7 9
LOCAL MINIMUM CELL
1 AT (2, 2)
2 AT (3, 3)
SMALLEST LMC = 1

Example 2
INPUT:
M = 5
N = 5
Enter the numbers:
9 9 9 9 9 9 5 7 4 9 9 8 3 8 9 9 6 7 5 9 9 9 9 9 9
OUTPUT:
THE MATRIX IS:
9 9 9 9 9
9 5 7 4 9
9 8 3 8 9
9 6 7 5 9
9 9 9 9 9
LOCAL MINIMUM CELL
5 AT (2, 2)
4 AT (2, 4)
3 AT (3, 3)
6 AT (4, 2)
5 AT (4, 4)
SMALLEST LMC = 3

Example 3
INPUT:
M = 4
N = 4
Enter the numbers:
1 2 3 4 5 6 7 8 9 7 6 5 4 3 2
OUTPUT:
THE MATRIX IS:
1 2 3 4
5 6 7 8
9 8 7 6
5 4 3 2
NO LOCAL MINIMUM CELL

Example 4
INPUT:
M = 3
N = 12
OUTPUT:
INVALID INPUT

import java.util.Scanner;
class LocalMinimumCell{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("M = ");
        int m = in.nextInt();
        System.out.print("N = ");
        int n = in.nextInt();
        if(m < 4 || n < 4 || m > 9 || n > 9){
            System.out.println("INVALID INPUT");
            return;
        }
        int h[][] = new int[m][n];
        System.out.println("Enter the numbers:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                h[i][j] = in.nextInt();
            }
        }
        System.out.println("THE MATRIX IS:");
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                System.out.print(h[i][j] + "\t");
            }
            System.out.println();
        }
        boolean isLocal = false;
        String list = "";
        int smallest = h[0][0];
        for(int i = 1; i < m - 1; i++){
            for(int j = 1; j < n - 1; j++){
                int up = h[i - 1][j];
                int down = h[i + 1][j];
                int left = h[i][j - 1];
                int right = h[i][j + 1];
                int cur = h[i][j];
                if(cur < up && cur < down && cur < left && cur < right){
                    if(!isLocal)
                        smallest = cur;
                    isLocal = true;
                    list += cur + " AT (" + (i + 1) + ", " + (j + 1) + ")" + "\n";
                    if(smallest > cur)
                        smallest = cur;
                }
            }
        }
        if(isLocal){
            System.out.println("LOCAL MINIMUM CELL");
            System.out.println(list);
            System.out.println("SMALLEST LMC = " + smallest);
        }
        else
            System.out.println("NO LOCAL MINIMUM CELL");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *