about summary refs log tree commit diff stats
path: root/algorithms/java/DFS.java
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/java/DFS.java')
-rw-r--r--algorithms/java/DFS.java7
1 files changed, 4 insertions, 3 deletions
diff --git a/algorithms/java/DFS.java b/algorithms/java/DFS.java
index ff4567d..e0da681 100644
--- a/algorithms/java/DFS.java
+++ b/algorithms/java/DFS.java
@@ -24,6 +24,9 @@ public class DFS {
                 || curx > maze.length - 1 || cury > maze[0].length - 1)
                 continue;
 
+            if (visited[curx][cury])
+                continue;
+
             // Marker cells.
             if (maze[curx][cury] == '$')
                 System.out.print("|");
@@ -43,9 +46,7 @@ public class DFS {
             if (maze[curx][cury] == '$')
                 System.exit(0);
 
-            if (visited[curx][cury]) {
-                continue;
-            } else if (maze[curx][cury] == '.') {
+            if (maze[curx][cury] == '.') {
                 visited[curx][cury] = true;
                 traverse(curx, cury, maze, visited);
                 visited[curx][cury] = false;