about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-11-03 17:13:15 +0530
committerAndinus <andinus@nand.sh>2021-11-03 17:13:15 +0530
commit5a1e12748274ffc87631124e6ae0d1a0a9f6c2e9 (patch)
tree8daf59d91a4d81002fa85f2f0f9d32100b1b7af9
parentdbb7b95b35a2be5af0e2068129cd8af2be827503 (diff)
downloadfornax-5a1e12748274ffc87631124e6ae0d1a0a9f6c2e9.tar.gz
java/DFS: Output in Fornax format
-rw-r--r--algorithms/java/DFS.java91
1 files changed, 47 insertions, 44 deletions
diff --git a/algorithms/java/DFS.java b/algorithms/java/DFS.java
index 671bfa8..61b2027 100644
--- a/algorithms/java/DFS.java
+++ b/algorithms/java/DFS.java
@@ -1,50 +1,46 @@
-public class CG_pathfinder {
-    static int paths = 0;
-
-    private static final int[][] dir = new int[][]{
-        {0, 1}, //right
-        {1, 0}, //down
-        {0, -1}, //left
-        {-1, 0} //up
+public class DFS {
+    private static final int[][] directions = new int[][]{
+        {+0, +1}, // Right.
+        {+1, +0}, // Down.
+        {+0, -1}, // Left.
+        {-1, +0}, // Up.
     };
 
     static void traverse(int x, int y, char[][] maze, boolean[][] visited) {
         int curx;
         int cury;
-        for (int i = 0; i < 4; i++) {
-            // Print the maze at every iteration.
-            for(int j = 0; j < maze.length; j++)
-                for(int k = 0; k < maze[j].length; k++)
-                    if (visited[j][k])
-                        System.out.print("x");
-                    else
-                        System.out.print(maze[j][k]);
-            System.out.print(" ");
 
-            curx = x;
-            cury = y;
-
-            curx += dir[i][0];
-            cury += dir[i][1];
+        for (int i = 0; i < 4; i++) {
+            curx = x + directions[i][0];
+            cury = y + directions[i][1];
 
+            // Out of bounds check.
             if (curx < 0 || cury < 0
                 || curx > maze.length - 1 || cury > maze.length - 1)
-                continue; //optional?       //for square mazes
+                continue;
 
-            if (maze[curx][cury] == '$') {
-                paths++;
+            // Marker cells.
+            if (maze[curx][cury] == '$')
                 System.out.print("|");
-                // Print the maze at every iteration.
-                for(int j = 0; j < maze.length; j++)
-                    for(int k = 0; k < maze[j].length; k++)
-                        if (visited[j][k])
-                            System.out.print("x");
-                        else
-                            System.out.print(maze[j][k]);
-                System.out.print(" ");
-            } else if (maze[curx][cury] == 'x' || visited[curx][cury])
+            else if (maze[curx][cury] == '#')
+                System.out.print("!");
+
+            // Print the maze on every iteration.
+            for(int j = 0; j < maze.length; j++)
+                for(int k = 0; k < maze[j].length; k++)
+                    if (j == curx && k == cury)
+                        System.out.print("@");
+                    else
+                        System.out.print(visited[j][k] ? "-" : maze[j][k]);
+            System.out.println();
+
+            // Found a solution, exiting.
+            if (maze[curx][cury] == '$')
+                System.exit(0);
+
+            if (visited[curx][cury]) {
                 continue;
-            else if (maze[curx][cury] == '_') {
+            } else if (maze[curx][cury] == '.') {
                 visited[curx][cury] = true;
                 traverse(curx, cury, maze, visited);
                 visited[curx][cury] = false;
@@ -54,20 +50,27 @@ public class CG_pathfinder {
 
     public static void main(String[] args) {
         char[][] maze = {
-            {'*', '#', '_'},
-            {'_', '_', '_'},
-            {'_', '_', '$'}
+            {'.', '#', '.'},
+            {'.', '.', '.'},
+            {'.', '.', '$'}
         };
 
-        int[] start = {0, 0};
         boolean[][] visited = new boolean[maze.length][maze[0].length];
-        for (int i = 0; i< maze.length; i++)
-            for (int j = 0; j<maze.length; j++)
+
+        for (int i = 0; i < maze.length; i++)
+            for (int j = 0; j < maze[i].length; j++)
                 visited[i][j] = false;
 
-        System.out.println(String.format("%d:%d", maze.length, maze[0].length));
-        visited[0][0] = true;
-        traverse(start[0], start[1], maze, visited);
+        System.out.println(String.format("rows:%d cols:%d", maze.length, maze[0].length));
+
+        // Print the maze.
+        for(int j = 0; j < maze.length; j++)
+            for(int k = 0; k < maze[j].length; k++)
+                    System.out.print(maze[j][k]);
         System.out.println();
+
+        // Start at 0,0.
+        visited[0][0] = true;
+        traverse(0, 0, maze, visited);
     }
 }
ffc ^
cf66462 ^


2349b7d ^



cf66462 ^
62946ff ^





9e3b602 ^
62946ff ^



cf66462 ^


62946ff ^



2349b7d ^
257affc ^
2349b7d ^


257affc ^


2349b7d ^


24196d2 ^
a782b70 ^





257affc ^




cf66462 ^
257affc ^





cf66462 ^






257affc ^

2349b7d ^
b60999c ^




































1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156