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
Code Cleaning Tips
getMazePath
method can be cleaned. In 1stif
you are checking if you have reached the goal or not. Now move the checking condition to another method and returntrue
if you have reached the goal.- 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.
- No need for maintaining a
Stack
, since you are not using any of Stack's method but onlyadd
, which you can also use with aList
. - Override
toString
method inCoordinate
class. And get rid of longS.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
Code Cleaning Tips
getMazePath
method can be cleaned. In 1stif
you are checking if you have reached the goal or not. Now move the checking condition to another method and returntrue
if you have reached the goal.- 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.
- No need for maintaining a
Stack
, since you are not using any of Stack's method but onlyadd
, which you can also use with aList
. - Override
toString
method inCoordinate
class. And get rid of longS.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
Code Cleaning Tips
getMazePath
method can be cleaned. In 1stif
you are checking if you have reached the goal or not. Now move the checking condition to another method and returntrue
if you have reached the goal.- 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.
- No need for maintaining a
Stack
, since you are not using any of Stack's method but onlyadd
, which you can also use with aList
. - Override
toString
method inCoordinate
class. And get rid of longS.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
Code Cleaning Tips
getMazePath
method can be cleaned. In 1stif
you are checking if you have reached the goal or not. Now move the checking condition to another method and returntrue
if you have reached the goal.- 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.
- No need for maintaining a
Stack
, since you are not using any of Stack's method but onlyadd
, which you can also use with aList
. - Override
toString
method inCoordinate
class. And get rid of longS.o.p
s.
Good Luck!