Search code examples
javamaze

Path in a maze(2-D Array)


Question: Given a maze as input alongwith the entry and exit points, find whether a path exists that leads from the entry point to the exit point. The maze is entered as char[][] with each value being either '+' or ' '(i.e. space). A '+' denotes a wall where as space denotes that their is a way to proceed. Note that we possible moves at any point are limited to the 4 directions, i.e., no diagonal moves are allowed. A sample maze looks like {

{'+','+','+','+','+','+','+','+','+','+','+'},
{'+',' ',' ',' ',' ','+',' ','+',' ',' ',' '},
{'+',' ','+',' ','+','+',' ',' ',' ','+','+'},
{'+',' ','+',' ',' ',' ',' ','+',' ','+','+'},
{'+',' ',' ',' ','+','+',' ','+',' ',' ','+'},
{'+','+',' ','+','+','+','+','+','+','+','+'}};

If the entry point is {5,2} and the exit point is {1,10} they are provided as input as "5:2,1:10".

Problem is: This code returns true even if path doesn't exist. What's wrong in this?

For instance, it returns true for this maze as well with input 10:7,1:10:

{
{'+','+','+','+','+','+','+','+','+','+','+'};
{'+',' ',' ',' ',' ',' ',' ',' ',' ','+',' '};
{'+',' ','+',' ','+','+',' ','+',' ','+','+'};
{'+',' ','+',' ',' ',' ',' ','+',' ','+','+'};
{'+','+','+',' ','+','+',' ','+',' ',' ','+'};
{'+',' ',' ',' ','+','+',' ',' ',' ',' ','+'};
{'+','+','+','+','+','+','+','+','+',' ','+'};
{'+',' ',' ',' ','+','+',' ',' ','+',' ','+'};
{'+',' ','+','+',' ',' ','+',' ',' ',' ','+'};
{'+',' ',' ',' ',' ',' ','+',' ','+','+','+'};
{'+','+','+','+','+','+','+',' ','+','+','+'}}

Here goes the code

public class MazePath {

    static char[][] testcase11 = {
{'+','+','+','+','+','+','+','+','+','+','+'};
{'+',' ',' ',' ',' ',' ',' ',' ',' ','+',' '};
{'+',' ','+',' ','+','+',' ','+',' ','+','+'};
{'+',' ','+',' ',' ',' ',' ','+',' ','+','+'};
{'+','+','+',' ','+','+',' ','+',' ',' ','+'};
{'+',' ',' ',' ','+','+',' ',' ',' ',' ','+'};
{'+','+','+','+','+','+','+','+','+',' ','+'};
{'+',' ',' ',' ','+','+',' ',' ','+',' ','+'};
{'+',' ','+','+',' ',' ','+',' ',' ',' ','+'};
{'+',' ',' ',' ',' ',' ','+',' ','+','+','+'};
{'+','+','+','+','+','+','+',' ','+','+','+'}}

    static String testcase12 = "10:7,1:10";

    // Getting start and end points from testcase12
    String[] parts1 = testcase12.split(":");
    String[] parts2 = parts1[1].split(",");

    int startX = Integer.valueOf(parts1[0]);
    int startY = Integer.valueOf(parts2[0]);

    int endX = Integer.valueOf(parts2[1]);
    int endY = Integer.valueOf(parts1[2]);

    static char [][] maze = testcase11;
    int[][] visited;
    int row, col;
    char d; 
    int result;

    public static void main(String[] args) {        
            MazePath testInstance = new MazePath();
            boolean result = testInstance.findPath(testcase11);
            System.out.print("Result is "+result);      
    }

    public boolean findPath(char[][] m)
    {
        row = maze.length;
        col = maze[0].length;

        visited = new int[row][col];
        d = 'o';
        result = 0;

        //System.out.println("Enter maze elements row wise, don't give extra characters (just '+' or ' ' without any ',' or ';')");

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col ;j++)
            {
                if(maze[i][j] == '+')
                    maze[i][j] = 1;
                else if((maze[i][j] == ' '))
                    maze[i][j] = 0;
                visited[i][j] = -5;
            }
        }

        // 5 means visited, -5 means not visited
        visited[startX][startY] = 5;

        path(startX, startY, d);
        if(result == 0)
            return false;
        else
            return true;

    }

    public int path(int startX, int startY, char d)
    {
        if(d != 'd' && startX - 1 >= 0)
        {
            if(startX - 1 == endX && startY == endY)
            {
                result = 1;
                return 1;
            }
            else if(maze[startX - 1][startY] == 0)
            {
                if(visited[startX-1][startY] == -5)
                {
                    visited[startX-1][startY] = 5;
                    d = 'u';
                    path(startX-1, startY, d);
                }
            }
        }
        if(d != 'u' && startX + 1 <= row)
        {
            if(startX + 1 == endX && startY == endY)
            {
                result = 1;
                return 1;
            }
            if(maze[startX+1][startY] == 0)
            {
                if(visited[startX+1][startY] == -5)
                {
                    visited[startX+1][startY]=5;
                    d = 'd';
                    path(startX+1, startY, d);
                }
            }
        }
        if(d != 'r' && startY-1 >= 0)
        {
            if(startX == endX && startY-1 == endY)
            {
                result=1;
                return 1;
            }
            if(maze[startX][startY-1]==0)
            {
                if(visited[startX][startY-1]==-5)
                {
                    visited[startX][startY-1] = 5;
                    d = 'l';
                    path(startX,startY-1,d);
                }
            }
        }
        if(d != 'l' && startY+1 <= col)
        {
            if(startX == endX && startY+1 == endY)
            {
                result=1;
                return 1;
            }
            if(maze[startX][startY+1] == 0)
            {
                if(visited[startX][startY+1] == -5)
                {
                    visited[startX][startY+1] = 5;
                    d = 'r';
                    path(startX, startY+1, d);
                }
            }
        }
        return 0;
    }
}

Solution

  • Improvised version of your code.
    
        public class MazePath {
    
        static char[][] testcase11 = {
            {'+','+','+','+','+','+','+','+','+','+','+'},
            {'+',' ',' ',' ',' ',' ',' ',' ',' ','+',' '},
            {'+',' ','+',' ','+','+',' ','+',' ','+','+'},
            {'+',' ','+',' ',' ',' ',' ','+',' ','+','+'},
            {'+','+','+',' ','+','+',' ','+',' ',' ','+'},
            {'+',' ',' ',' ','+','+',' ',' ',' ',' ','+'},
            {'+','+','+','+','+','+','+','+','+',' ','+'},
            {'+',' ',' ',' ','+','+',' ',' ','+',' ','+'},
            {'+',' ','+','+',' ',' ','+',' ',' ',' ','+'},
            {'+',' ',' ',' ',' ',' ','+',' ','+','+','+'},
            {'+','+','+','+','+','+','+',' ','+','+','+'}};
        static String testcase12 = "10:7,1:10";
    
        public static void main(String[] args) {        
            MazePath testInstance = new MazePath();
            boolean result = testInstance.findPath(testcase11,testcase12);
            System.out.print("Result is "+result);      
        }   
    
        public boolean findPath(char[][] maze, String points)
        {
            char [][] inputMaze = maze;
    
            int row = maze.length;
            int col = maze[0].length;
    
            // Getting start and end points
            String[] parts1 = points.split(":");
            String[] parts2 = parts1[1].split(",");
    
            int startX = Integer.valueOf(parts1[0]);
            int startY = Integer.valueOf(parts2[0]);
    
            int endX = Integer.valueOf(parts2[1]);
            int endY = Integer.valueOf(parts1[2]);
    
            char[][] visited = new char[row][col];      
    
            boolean result = path(inputMaze, startX, startY, endX, endY, visited);
            if(!result)
                return false;
            else
                return true;
        }
    
    
        public boolean path(char[][] inputMaze,int startX, int startY, int endX, int endY, char[][] visited)
        {
            if((startX == endX) && (startY == endY))
            {
                return true;
            }
            if((startX > inputMaze.length-1) || (startY > inputMaze[0].length-1) && (startX < 0 || startY < 0))
            {
                return false;
            }
            if((inputMaze[startX][startY] == ' ') && (visited[startX][startY] != '*'))
            {
                visited[startX][startY] = '*';
            }
            else
            {
                return false;
            }
            boolean u = path(inputMaze, startX-1, startY, endX, endY, visited);
            boolean d = path(inputMaze, startX+1, startY, endX, endY, visited);
            boolean r = path(inputMaze, startX, startY+1, endX, endY, visited);
            boolean l = path(inputMaze, startX, startY-1, endX, endY, visited);
    
            return u || d|| l || r;
        }   
    }