From 8cef86f0eb8b46b0ed2d7c37fa216890300249f6 Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 3 Nov 2021 22:22:57 +0530 Subject: 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. --- algorithms/java/DFS.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'algorithms') 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; -- cgit 1.4.1-2-gfad0