fork(1) download
  1. /**
  2.  * Find count of possible paths in the maze
  3.  * @author PRATEEK
  4.  */
  5. class CountPaths {
  6.  
  7. private static int rowLen, colLen, count;
  8. public static void paths(int[][] maze, int row, int col) {
  9. if (row == rowLen && col == colLen) {
  10. count++;
  11. return;
  12. }
  13. /*// move left
  14. if (col > 0 && maze[row][col - 1] == 1)
  15. paths(maze, row, col - 1);*/
  16.  
  17. // move right
  18. if (col < colLen && maze[row][col + 1] == 1)
  19. paths(maze, row, col + 1);
  20.  
  21. /*// move up
  22. if (row > 0 && maze[row - 1][col] == 1)
  23. paths(maze, row - 1, col);*/
  24.  
  25. // move down
  26. if (row < rowLen && maze[row + 1][col] == 1)
  27. paths(maze, row + 1, col);
  28.  
  29. }
  30.  
  31. public static void main(String[] args) {
  32. int[][] maze = {
  33. {1, 1, 0, 0, 1, 1 },
  34. {1, 1, 1, 1, 0, 0 },
  35. {1, 0, 0, 0, 1, 0 },
  36. {1, 1, 1, 1, 0, 0 },
  37. {1, 0, 1, 0, 0, 0 },
  38. {1, 1, 1, 1, 1, 1 }
  39. };
  40.  
  41. rowLen = maze.length-1;
  42. colLen = maze[0].length-1;
  43.  
  44. paths(maze,0,0);
  45. System.out.println("Number of Paths: "+count);
  46. }
  47. }
  48.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Number of Paths: 2