about summary refs log tree commit diff stats
path: root/algorithms/java
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/java')
-rw-r--r--algorithms/java/DFS.java44
1 files changed, 23 insertions, 21 deletions
diff --git a/algorithms/java/DFS.java b/algorithms/java/DFS.java
index 5357a7d..671bfa8 100644
--- a/algorithms/java/DFS.java
+++ b/algorithms/java/DFS.java
@@ -12,6 +12,15 @@ public class CG_pathfinder {
         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;
 
@@ -23,13 +32,16 @@ public class CG_pathfinder {
                 continue; //optional?       //for square mazes
 
             if (maze[curx][cury] == '$') {
-                System.out.println("Path Found");
                 paths++;
-                for(int k = 0; k < maze.length; k++) {
-                    for(int j = 0; j < maze.length; j++)
-                        System.out.print(visited[k][j]);
-                    System.out.println();
-                }
+                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])
                 continue;
             else if (maze[curx][cury] == '_') {
@@ -42,30 +54,20 @@ public class CG_pathfinder {
 
     public static void main(String[] args) {
         char[][] maze = {
-            {'*', '#', 'x'},
+            {'*', '#', '_'},
             {'_', '_', '_'},
             {'_', '_', '$'}
         };
-        int[] start = {0, 0};
-
-        System.out.println("Solving for the maze: ");
-        boolean[][] visited = new boolean[maze.length][maze.length];
-
-        for (int i = 0; i<maze.length; i++){
-            for (int j = 0; j<maze[i].length; j++)
-                System.out.print(maze[i][j] + " "); // Printing maze
-            System.out.println("");
-        }
 
+        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++)
                 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);
-        if (paths == 0)
-            System.out.println("no paths found");
-        else
-            System.out.println(paths);
+        System.out.println();
     }
 }
n207'>207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306