I am trying to solve a variant of the very popular question in which we find the number of distinct valid paths on a directed graph. The graph's nodes make up a m x n lattice (grid), and the edges only lead down and right. The goal is to find how many distinct paths start at the top left corner of and end in the bottom right.
So one possible path might be down m, right n.
In this variant of the problem, there is a complication. A rectangular area inside the lattice is blocked -- no paths can go through it. (The edges that would normally lead to these nodes don't exist)
I am using Dynamic Programming to solve with a time complexity of \$O(m*n)\$.
It works pretty good with small value for rows and columns but for bigger values it throws OutOfMemoryError
.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GeekForGeeks{
public static void main(String[] args) throws NumberFormatException,
IOException {
// TODO Auto-generated method stub
BufferedReader inp = new BufferedReader(
new InputStreamReader(System.in));
String[] s1 = inp.readLine().split(" ");
int row = Integer.parseInt(s1[0]);
int col = Integer.parseInt(s1[1]); // Input for the value of row &
// col
String[] s2 = inp.readLine().split(" ");
int r1 = Integer.parseInt(s2[0]);
int c1 = Integer.parseInt(s2[1]);
int r2 = Integer.parseInt(s2[2]);
int c2 = Integer.parseInt(s2[3]); // Input for the value of row &
// column of blocked path
int[][] A = new int[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
A[i][j] = 1;
for (int i = r1 - 1; i < r2; i++)
for (int j = c1 - 1; j < c2; j++)
A[i][j] = 0;
int answer = numOfPaths(A, row, col);
System.out.println(answer);
}
}
private static int numOfPaths(int A[][], int row, int col) {
long mod = (long) Math.pow(10, 9) + 7;
int[][] B = new int[row][col]; // new matrix to store number of paths
// if starting point blocked then no path is there to reach right down
if (A[0][0] == 0) {
return 0;
} else {
int i, j;
for (i = 0; i < col; i++) {
if (A[0][i] == 0)
break; // exit from the for loop, and set remaining elements
// the first row set to ZERO
B[0][i] = A[0][i];
}
while (i < col)
B[0][i++] = 0; // When any starting row is blocked then all the
// row down will also be blocked and
// have no path to be reached from starting
// point.
for (i = 1; i < row; i++) {
if (A[i][0] == 0)
break;// exit from the for loop, and set remaining elements
// in the first column set to ZERO
B[i][0] = A[i][0];
}
while (i < row)
B[i++][0] = 0; // When any column is blocked all starting column
// towards right will be blocked
for (i = 1; i < row; i++)
for (j = 1; j < col; j++)
if (A[i][j] != 0)
B[i][j] = (int) (((B[i - 1][j] % mod) + (B[i][j - 1] % mod)) % mod);
// otherwise, we enter the sum of the value above and left
return B[row - 1][col - 1]; // return the last column and row.
}
}