Skip to content
Advertisement

How to check if 2 elements are adjacent to one another in 2D array

I’m trying to figure out how to take in 2 integer parameters, an initial integer referred to as “start” and a second integer referred to as “destination”. I’m wanting to use both parameters from my method, first checking if the starting integer is within the matrix and then checking if the destination integer is adjacent within the 4 elements around it – (north, east, south, west).

Example 1:

example1

If the starting integer is (6), check whether the destination integer (7) is adjacent to the starting integer. If true then do something.

Example 2:

example1

In this case if the starting integer = 4 and the destination integer = 2. The program would not consider these elements adjacent.

Initialising array:

int[][] matrix = {{0,1,2,}, 
                  {3,4,5,}, 
                  {6,7,8}};

Check method:

public static  double check(int start, int destination)
{

    for(int row = 0; row < matrix.length; row++)
    {
        for(int col = 0; col < matrix[0].length; col++)
        {
            // check if the start integer is in the matrix
            if(matrix[row][col] == start)
            {
                // check if destination integer is adjacent to starting integer:
                if (destination is adjacent to start)
                {
                   // do something
                }

            }
        }
     }
}

Bear in mind, the matrix won’t have duplicate numbers and will always stay the same.

How would I go about checking this?

I’ve spent a few hours looking at similar StackOverflow posts but I really can’t seem to grasp the examples given. Could someone guide me through it?

Advertisement

Answer

If your start element is in the matrix there are 4 possible spots to check for your destination element: LEFT, RIGHT, UP, DOWN. You could just add a variable which is set by default to false and check the 4 spots keeping in mind to don’t get out of bounds:

public static double check(int start, int destination) {
    for (int row = 0; row < matrix.length; row++) {
        for (int col = 0; col < matrix[0].length; col++) {
            // check if the start integer is in the matrix
            if (matrix[row][col] == start) {
                // check if destination integer is adjacent to starting integer:
                boolean destIsAdj = false;
                //left
                if (col - 1 > 0 && matrix[row][col-1] == destination) 
                    destIsAdj = true;
                //right
                else if (col + 1 < matrix[0].length && matrix[row][col+1] == destination) 
                    destIsAdj = true;
                //up
                else if (row - 1 > 0 && matrix[row-1][col] == destination) 
                    destIsAdj = true;
                //down
                else if (row + 1 < matrix.length && matrix[row+1][col] == destination) 
                    destIsAdj = true;

                if (destIsAdj){
                    // adjacent! do something
                }
                else {
                    // not adjacent! do something else
                }
            }
        }
    }
    return 0.0;
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement