-
Notifications
You must be signed in to change notification settings - Fork 7
Adds DFS, BFS and Dijkstra's algorithm solutions for "The Maze II" #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Issue: #56 |
As we use PQ to find the closest node, this method is obsolete now.
|
Hello @ErdemT09, I have done some improvements in Dijkstra implementation.
|
|
|
||
| if(maze[x][y]==-1) continue; | ||
|
|
||
| maze[x][y] = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reccommend moving -1 to a constant called "VISITED" for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still suggest doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oke, it is done. Thanks for the suggestion.
| if (startPoint[0] < 0) | ||
| break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the function of this statement here. Can you please explain that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is also a resideue. I just removed it.
| return (nextX >= 0 && nextY >= 0 | ||
| && nextX < maze.length | ||
| && nextY < maze[0].length | ||
| && maze[nextX][nextY] != 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we check for this condition unlike in the previous two solutions?
maze[nextX][nextY] == 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have understood the reason why we don't do this.
In the previous solution, the tiles that we come across are either empty or a wall. If empty, we move through them. If wall, we stop. Here, we have a three valued logic: A tile is either empty but visited, empty or a wall. We should be able to move through an empty tile even if it is visited. At the end, we can't stop and branch off there, but whilst moving to another valid tile, we should be able to pass them.
After all, we start the canPass loop from an already visited tile. This means we have to consider -1 as a valid passable tile value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation Erdem. Appreciated.
|
I have accidentally deleted the branch. I should have been more careful. |
ErdemT09
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved. A good question in terms of graph theory.
resolves #56
This question is a version of #282, but this time we are asked to find the shortest path to the target.
Dijkstra's algorithm appears to be the fastest with ~5ms and the way it searches is almost the same as the BFS.
But Dijkstra starts with detecting the node which has the least distance from the starting point.
Priority Queueis a perfect match to poll the closest node.