about summary refs log tree commit diff stats
path: root/algorithms/java
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-11-03 21:20:30 +0530
committerAndinus <andinus@nand.sh>2021-11-03 21:20:30 +0530
commit9c398885517a189cb0063dd2994b5cbf33e412cb (patch)
tree89679f847cc2ccd06b8dd5b9765537f2d04863b9 /algorithms/java
parentc2d8da314510274dab62d0be92f8ae6138c01ad8 (diff)
downloadfornax-9c398885517a189cb0063dd2994b5cbf33e412cb.tar.gz
java/DFS: Chose neighbor randomly
This does mean that we might end up taking a longer path but also the
inverse!
Diffstat (limited to 'algorithms/java')
-rw-r--r--algorithms/java/DFS.java10
1 files changed, 9 insertions, 1 deletions
diff --git a/algorithms/java/DFS.java b/algorithms/java/DFS.java
index 8d1e341..e2f866e 100644
--- a/algorithms/java/DFS.java
+++ b/algorithms/java/DFS.java
@@ -1,3 +1,7 @@
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
 public class DFS {
     static int[][] directions = new int[][]{
         {+0, +1}, // Right.
@@ -7,7 +11,11 @@ public class DFS {
     };
 
     static void traverse(int x, int y, char[][] maze, boolean[][] visited) {
-        for (int i = 0; i < 4; i++) {
+        // Move in random direction.
+        List<Integer> l = Arrays.asList(new Integer[]{0, 1, 2, 3});
+        Collections.shuffle(l);
+
+        for (int i: l) {
             int curx = x + directions[i][0];
             int cury = y + directions[i][1];