Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

First of all I don't think the time complexity is O(n!). You are doing DFS(unknowingly). So the time complexity would be O(n + m) O(n + m) and AFAIK you can't solve maze problem less than that unless you are using BFS. But nevertheless it will not make any difference.

Now your code has some bug, I think you didn't consider that way and it's ok if you are a beginner like me. Try to change the maze matrix to

int[][] m = {{1, 1, 1},
 {1, 0, 1},
 {1, 1, 0}, 
 {0, 1, 1}};

and you will get the picture. Maze can be solved but still it's not printing anything. So what is the solution? BACK-TRACK.

Moreover you are only taking account of possibilities of going right and down, why not up and left?

You have hard-coded that the start point will be always (0,0) and end point will be (N-1, N-1)... yeah your puzzle says that but this will not be same for every puzzle right? So think of a general solving technique...

SPOILER ALERT

Web resource

My solution

Code Cleaning Tips

  1. getMazePath method can be cleaned. In 1st if you are checking if you have reached the goal or not. Now move the checking condition to another method and return true if you have reached the goal.
  2. In for loop(no need of this if you are using recursion) you are checking if you are in the maze and path exists or not... see how you are repeating yourself for every condition... think of a general solution.
  3. No need for maintaining a Stack, since you are not using any of Stack's method but only add, which you can also use with a List.
  4. Override toString method in Coordinate class. And get rid of long S.o.p s.

Good Luck!

First of all I don't think the time complexity is O(n!). You are doing DFS(unknowingly). So the time complexity would be O(n + m) and AFAIK you can't solve maze problem less than that unless you are using BFS. But nevertheless it will not make any difference.

Now your code has some bug, I think you didn't consider that way and it's ok if you are a beginner like me. Try to change the maze matrix to

int[][] m = {{1, 1, 1},
 {1, 0, 1},
 {1, 1, 0}, 
 {0, 1, 1}};

and you will get the picture. Maze can be solved but still it's not printing anything. So what is the solution? BACK-TRACK.

Moreover you are only taking account of possibilities of going right and down, why not up and left?

You have hard-coded that the start point will be always (0,0) and end point will be (N-1, N-1)... yeah your puzzle says that but this will not be same for every puzzle right? So think of a general solving technique...

SPOILER ALERT

Web resource

My solution

Code Cleaning Tips

  1. getMazePath method can be cleaned. In 1st if you are checking if you have reached the goal or not. Now move the checking condition to another method and return true if you have reached the goal.
  2. In for loop(no need of this if you are using recursion) you are checking if you are in the maze and path exists or not... see how you are repeating yourself for every condition... think of a general solution.
  3. No need for maintaining a Stack, since you are not using any of Stack's method but only add, which you can also use with a List.
  4. Override toString method in Coordinate class. And get rid of long S.o.p s.

Good Luck!

First of all I don't think the time complexity is O(n!). You are doing DFS(unknowingly). So the time complexity would be O(n + m) and AFAIK you can't solve maze problem less than that unless you are using BFS. But nevertheless it will not make any difference.

Now your code has some bug, I think you didn't consider that way and it's ok if you are a beginner like me. Try to change the maze matrix to

int[][] m = {{1, 1, 1},
 {1, 0, 1},
 {1, 1, 0}, 
 {0, 1, 1}};

and you will get the picture. Maze can be solved but still it's not printing anything. So what is the solution? BACK-TRACK.

Moreover you are only taking account of possibilities of going right and down, why not up and left?

You have hard-coded that the start point will be always (0,0) and end point will be (N-1, N-1)... yeah your puzzle says that but this will not be same for every puzzle right? So think of a general solving technique...

SPOILER ALERT

Web resource

My solution

Code Cleaning Tips

  1. getMazePath method can be cleaned. In 1st if you are checking if you have reached the goal or not. Now move the checking condition to another method and return true if you have reached the goal.
  2. In for loop(no need of this if you are using recursion) you are checking if you are in the maze and path exists or not... see how you are repeating yourself for every condition... think of a general solution.
  3. No need for maintaining a Stack, since you are not using any of Stack's method but only add, which you can also use with a List.
  4. Override toString method in Coordinate class. And get rid of long S.o.p s.

Good Luck!

Source Link

First of all I don't think the time complexity is O(n!). You are doing DFS(unknowingly). So the time complexity would be O(n + m) and AFAIK you can't solve maze problem less than that unless you are using BFS. But nevertheless it will not make any difference.

Now your code has some bug, I think you didn't consider that way and it's ok if you are a beginner like me. Try to change the maze matrix to

int[][] m = {{1, 1, 1},
 {1, 0, 1},
 {1, 1, 0}, 
 {0, 1, 1}};

and you will get the picture. Maze can be solved but still it's not printing anything. So what is the solution? BACK-TRACK.

Moreover you are only taking account of possibilities of going right and down, why not up and left?

You have hard-coded that the start point will be always (0,0) and end point will be (N-1, N-1)... yeah your puzzle says that but this will not be same for every puzzle right? So think of a general solving technique...

SPOILER ALERT

Web resource

My solution

Code Cleaning Tips

  1. getMazePath method can be cleaned. In 1st if you are checking if you have reached the goal or not. Now move the checking condition to another method and return true if you have reached the goal.
  2. In for loop(no need of this if you are using recursion) you are checking if you are in the maze and path exists or not... see how you are repeating yourself for every condition... think of a general solution.
  3. No need for maintaining a Stack, since you are not using any of Stack's method but only add, which you can also use with a List.
  4. Override toString method in Coordinate class. And get rid of long S.o.p s.

Good Luck!

lang-java

AltStyle によって変換されたページ (->オリジナル) /