Search code examples
validationmatrixmaze

Validation function for not crossing the boundaries of a matrix (maze)


Assume you have programmed a m x n matrix in Python. It is not possible to have a value outside the matrix. Assume you are something that is moving in the matrix (like in a maze) and that you can't cross the boundaries. While you are moving through the maze, you constantly consider your options in which way you can go. So for every step you need to check if you can go in every direction or if there are boundaries which you can't cross.

So consider a function that need to check if an input for the next step is possible. If the input is the position in the matrix (x,y), then it needs to check if it can move in every direction without crossing the boundaries of the matrix. So it need to check if the positions (x+1,y),(x-1,y),(x,y-1),(x,y+1) are still in the matrix.

You can make this function with a lot of if-statements like
if x-1 < 0: return False

elif y+1 > len(matrix): return False

You can make these if-statements for every directions, but it seems to me that it is to much work for just checking the input. Is there maybe a built-in function or a property of the matrix or a much more simpeler if-statement so you can check the input more easily?


Solution

  • It depends a little on what language you're using, but most often I write code to check adjacent positions like this (if we're not counting diagonals as adjacent):

    //given current position in x and y...
    
    int dx=1, dy=0; //first check to the right
    for (int i=0; i<4; i++)
    {
        int testx = dx, testy = dy; //remember current direction
        dx = -testy; dy = testx;    //next direction is rotated 90 degrees
        testx += x; testy += y;     //new position to test
        if (testx>=0 && testx<width && testy>=0 && testy<height)
        {
            //this position is within the matrix.  do other checks and stuff
        }
    }
    

    If you want to check diagonals too, then I often do something like this:

    //given current position in x and y...
    
    for (int dy=-1; dy<=1; ++dy)
    {
        for(int dx=-1; dx<=1; dx+=(dy==0?2:1))
        {
            int testx = x+dx, testy = y+dy; //new position to test
            if (testx>=0 && testx<width && testy>=0 && testy<height)
            {
                //this position is within the matrix.  do other checks and stuff
            }
        }
    }
    

    EDIT:

    Oooh, I just thought of a new way to do the 4-way adjacency case that I think I'll use from now on (you get an upvote!). This code generates the 4 adjacent positions in clockwise order:

    //given current position in x and y...
    
    for (int i=0; i<4; ++i)
    {
        int dse = (i>>i)&1;
        int dsw = (i^dse)&1;
        int testx = x+dsw-dse, testy = y+1-dsw-dse; //new position to test
        if (testx>=0 && testx<width && testy>=0 && testy<height)
        {
            //this position is within the matrix.  do other checks and stuff
        }
    }