Roopali Bansal
Roopali Bansal

Reputation: 673

How to find escape routes from one point to another in a matrix?

This is not a homework. It's just a practice question.

Given a matrix find the number of possible escape routes from (0,0) to (N,N). You cannot move diagonally.

A '0' position represents an open cell, while a '1' represents a blocked cell. I started my journey from (0,0) and had to reach (N,N).

Input format

First line is a single odd positive integer, T (<= 85), which indicates the size of the matrix. T lines follow, each containing T space separated numbers which are either '0' or '1'.

Output format

Output the number of ways in which I could have escaped from (0,0) to (N,N).

Sample Input

7
0 0 1 0 0 1 0
1 0 1 1 0 0 0
0 0 0 0 1 0 1
1 0 1 0 0 0 0 
1 0 1 1 0 1 0
1 0 0 0 0 1 0
1 1 1 1 0 0 0

Sample Output

4

According to my solution I have taken four directions - left (l), right(r), up(u), down(d).

The problem is that it is giving a wrong answer or a stackoverflow error. What is missing?

And is this the optimal solution to this question?

My Solution (Java)

import java.io.BufferedReader;
import java.io.InputStreamReader;

class testclass {
int no_of_escapes = 0 ;
int[][] arr;
int matrixlength;
public static void main(String[] args) throws Exception 
{

    testclass obj = new testclass();
    obj.checkpaths(0,0,"");
    System.out.print(obj.no_of_escapes);

}//main

testclass()
{
    try
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    matrixlength =Integer.parseInt(br.readLine());      
     arr = new int[matrixlength][matrixlength];
    for( int k = 0; k < matrixlength; k++){

        String str = br.readLine();
        int count = 0;
        for(int j=0 ; j< ((2*matrixlength)-1); j++){
            int v = (int)str.charAt(j) - 48;
            if(v == -16){}
            else{
            arr[k][count] = v;
            count++;
            }

        }//for j

    }//for k

}
catch(Exception e){}
}

public void checkpaths(int m, int n,String direction){

    if((m == matrixlength -1) && (n == matrixlength-1))
    {
        no_of_escapes = no_of_escapes +1;
        return;
    }

    if(!direction.equals("l"))
    {
        if(m < matrixlength && n < matrixlength)
            {
                if((n+1) < matrixlength )
                    {
                        if(arr[m][n+1]==0 )
                            {
                                checkpaths(m,n+1,"r");
                            }
                    }
            }
    }

    if(!direction.equals("u"))
    {
        if((m+1) < matrixlength )
        {
            if(arr[m+1][n]==0 )
            {
            checkpaths(m+1,n,"d");                  
            }
        }
    }

    if(!direction.equals("r"))
    {
        if(m < matrixlength && n < matrixlength)
            {
                if((n+1) < matrixlength )
                    {
                        if(arr[m][n+1]==0 )
                            {
                                checkpaths(m,n+1,"l");
                            }
                    }
            }
    }

    if(!direction.equals("d"))
    {
        if((m-1)>=0)
        {
            if(arr[m-1][n]==0 )
            {
            checkpaths(m-1,n,"u");                  
            }

        }

    }


}
}//class

Upvotes: 0

Views: 876

Answers (1)

Vincent van der Weele
Vincent van der Weele

Reputation: 13187

I would keep a second 2D array of booleans to mark the cells you already visited, as shown in the snippet below. I also simplified some other parts of the code, to reduce code-duplication.

Of course, you need to initialize visited in your constructor, just as you initialized arr, by using visited = new boolean[matrixLength][matrixLength].

int[][] arr;
boolean[][] visited;
final int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

public boolean isValid(int x, int y) {
    return 0 <= x && x < matrixLength 
        && 0 <= y && y < matrixLength 
        && arr[x][y] == 0
        && !visited[x][y];
}


public void checkPaths(int x, int y) {
    if (x == matrixLength-1 && y == matrixLength-1) {
        no_of_escaped++;
    } else {
        for (int[] d : directions) {
            if (isValid(x + d[0], y + d[1])) {
                visited[x + d[0]][y + d[1]] = true;
                checkPaths(x + d[0], y + d[1]);
                visited[x + d[0]][y + d[1]] = false;
            }
        }
    }
}

Upvotes: 2

Related Questions