From fad69ef7872e7fb25a0570adecf33f266439137a Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 2 Nov 2021 14:21:07 +0530 Subject: java/DFS: Make output parseable --- algorithms/java/DFS.java | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'algorithms') 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