-
Notifications
You must be signed in to change notification settings - Fork 7
Adds a DFS and a BFS solution for "The Maze" #282
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
|
Why is the helper method called "dps" and not "dfs"? |
No no. It is just a typo. Thanks for your attention. Fixing now. |
Fixes the dfs method name.
| int pry = nextY; | ||
| nextX += dirx; | ||
| nextY += diry; | ||
| result = result || !canPass(nextX, nextY) && dps(prx, pry); |
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 think using parenthesis here for the operators would make the code more readable.
As a note for those who do not know about the precedence of the operators.

Source: http://www.math.uaa.alaska.edu/~afkjm/cs110/handouts/IfStatements.pdf
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.
Nice idea to add this table.
Adds parenthesis to make it more readable.
| while (canPass(nextX, nextY) | ||
| && !visited[nextX][nextY]) { | ||
| int prx = nextX; | ||
| int pry = nextY; | ||
| nextX += dirx; | ||
| nextY += diry; | ||
| result = result || (!canPass(nextX, nextY) && dfs(prx, pry)); |
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.
Let's say that we have went left from some tile. This loop implies that we will also traverse the same from left to right until we see that the right tile is already visited. Isn't this to some degree duplicate unneeded calculation or am I seeing it wrong?
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 duplicated but I am not sure if it is unnecessary.
Somehow we have to distinguish the above-mentioned case and the case in Example-2.
It is one of the alternative ways.
We can change it when we find a more efficient way.
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.
We can perhaps put a simple statement like "Don't go right" to the method call. We can wrap this up in an integer or a boolean value.
|
I will also add BFS solution. Maybe it is much more appropriate as it can prevent dead-ends more efficiently. |
|
Here is what I meant: public boolean dfs(int x, int y, int arrivedFrom) {
if (x == destination[0] && y == destination[1]) {
return true;
}
visited[x][y] = true;
boolean result = false;
for (int i = 0; i<4; i++){
if((i == 0 && arrivedFrom == 3)
||(i == 1 && arrivedFrom == 2)
|| (i == 2 && arrivedFrom == 1)
|| (i == 3 && arrivedFrom == 0))
{
continue;
}
int dirx = directions[i][0];
int diry = directions[i][1];
int nextX = x + dirx;
int nextY = y + diry;
while (canPass(nextX, nextY)
&& !visited[nextX][nextY]) {
int prx = nextX;
int pry = nextY;
nextX += dirx;
nextY += diry;
result = result || !canPass(nextX, nextY) && dfs(prx, pry, i);
}
}
return result;
}This way, the program does not repeat itself, reducing the overall muda. |
|
In the BFS solution, we can perhaps implement the algorithm of not going back to the place we came from by changing the boolean matrix with a private final static boolean[] UNVISITED = {false, false, false, false};
private final static boolean[] RIGHT = {true, false, false, false};
private final static boolean[] LEFT = {false, true, false, false};
private final static boolean[] UP = {false, false, true, false};
private final static boolean[] DOWN = {false, false, false, true};
private boolean[][][] visited;We can work with |
|
I think the solutions are current quite fine. We can add other settings to them in the future if needed. |


Resolves #48
This solution is very important to grasp DFS and it is a kind of basis for The Maze II and The Maze III.
It is also a little bit different than the usual DFS problems as you cannot stop and change direction until you encounter a wall or the destination.
It is important to note that, you have to decide if DFS can pass over the next cell, not the current cell.
The most straightforward cases are noted in the problem statement (#48) for the first and second examples.
But, I think there is another case that is more veiled and pretty hard to see at the first glance.
I tried to draw the mentioned case:
In the above-mentioned case; you must pass at the first encounter but you have to stop after another turn of DFS (and in a different direction.)