/**
* Find count of possible paths in the maze
* @author PRATEEK
*/
class CountPaths {
private static int rowLen, colLen, count;
public static void paths(int[][] maze, int row, int col) {
if (row == rowLen && col == colLen) {
count++;
return;
}
/*// move left
if (col > 0 && maze[row][col - 1] == 1)
paths(maze, row, col - 1);*/
// move right
if (col < colLen && maze[row][col + 1] == 1)
paths(maze, row, col + 1);
/*// move up
if (row > 0 && maze[row - 1][col] == 1)
paths(maze, row - 1, col);*/
// move down
if (row < rowLen && maze[row + 1][col] == 1)
paths(maze, row + 1, col);
}
public static void main
(String[] args
) { int[][] maze = {
{1, 1, 0, 0, 1, 1 },
{1, 1, 1, 1, 0, 0 },
{1, 0, 0, 0, 1, 0 },
{1, 1, 1, 1, 0, 0 },
{1, 0, 1, 0, 0, 0 },
{1, 1, 1, 1, 1, 1 }
};
rowLen = maze.length-1;
colLen = maze[0].length-1;
paths(maze,0,0);
System.
out.
println("Number of Paths: "+count
); }
}
LyoqCiAqIEZpbmQgY291bnQgb2YgcG9zc2libGUgcGF0aHMgaW4gdGhlIG1hemUKICogQGF1dGhvciBQUkFURUVLCiAqLwogY2xhc3MgQ291bnRQYXRocyB7CgoJcHJpdmF0ZSBzdGF0aWMgaW50IHJvd0xlbiwgY29sTGVuLCBjb3VudDsKCXB1YmxpYyBzdGF0aWMgdm9pZCBwYXRocyhpbnRbXVtdIG1hemUsIGludCByb3csIGludCBjb2wpIHsKCQlpZiAocm93ID09IHJvd0xlbiAmJiBjb2wgPT0gY29sTGVuKSB7CgkJCWNvdW50Kys7CgkJCXJldHVybjsKCQl9CgkJLyovLyBtb3ZlIGxlZnQKCQlpZiAoY29sID4gMCAmJiBtYXplW3Jvd11bY29sIC0gMV0gPT0gMSkKCQkJcGF0aHMobWF6ZSwgcm93LCBjb2wgLSAxKTsqLwoKCQkvLyBtb3ZlIHJpZ2h0CgkJaWYgKGNvbCA8IGNvbExlbiAmJiBtYXplW3Jvd11bY29sICsgMV0gPT0gMSkKCQkJcGF0aHMobWF6ZSwgcm93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