-4

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;
    }
}
abhishek14d
  • 89
  • 2
  • 12
  • I have replaced `+` with `1` and ` ` with `0` in the maze array. – abhishek14d Aug 25 '13 at 21:31
  • 3
    1) Learn to debug your programs. Step it instruction by instruction and find the place where the algorithm behaves strangely. If you find the place but do not know why, then ask here back and you will find a quick answer. 2) If you do not want to do 1) as you should, the very minimum, would be providing an example (input, expected output vs actual output) of such error (instead of providing an example that is not an error) – SJuan76 Aug 25 '13 at 21:36
  • @SJuan76 I am not able to debug the issue. Also, now I have included a testcase which it fails, check out. – abhishek14d Aug 25 '13 at 21:46
  • 2
    @abhishek14d "I am not able to debug the issue." why can't you debug it? – Sean F Aug 26 '13 at 04:49
  • @SeanF nevermind, I have now solved it using a slightly different approach. – abhishek14d Aug 28 '13 at 12:46

1 Answers1

0
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;
    }   
}