about summary refs log tree commit diff stats
path: root/algorithms
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-11-03 22:22:57 +0530
committerAndinus <andinus@nand.sh>2021-11-03 22:22:57 +0530
commit8cef86f0eb8b46b0ed2d7c37fa216890300249f6 (patch)
tree4d50ebc636068bcc1aa0fc8f7c8bbbc2ac252a41 /algorithms
parentc9e3cb29fedcbe7e247d5abfb61bc4f0024ce5f4 (diff)
downloadfornax-8cef86f0eb8b46b0ed2d7c37fa216890300249f6.tar.gz
java/DFS: Don't walk on visited, Add DFS solutions, change colors
It didn't walk on visited grid but printed that as an iteration so it
seemed like it did.
Diffstat (limited to 'algorithms')
-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;