From e1e90993013b15b63409859fba8476de4da6924d Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 3 Nov 2021 21:34:02 +0530 Subject: java/DFS: Fix out of bounds check --- algorithms/java/DFS.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'algorithms') diff --git a/algorithms/java/DFS.java b/algorithms/java/DFS.java index e2f866e..ba3d8d8 100644 --- a/algorithms/java/DFS.java +++ b/algorithms/java/DFS.java @@ -21,7 +21,7 @@ public class DFS { // Out of bounds check. if (curx < 0 || cury < 0 - || curx > maze.length - 1 || cury > maze.length - 1) + || curx > maze.length - 1 || cury > maze[0].length - 1) continue; // Marker cells. @@ -40,8 +40,8 @@ public class DFS { System.out.println(); // Found a solution, exiting. - if (maze[curx][cury] == '$') - System.exit(0); + // if (maze[curx][cury] == '$') + // System.exit(0); if (visited[curx][cury]) { continue; @@ -57,7 +57,11 @@ public class DFS { char[][] maze = { {'.', '#', '.'}, {'.', '.', '.'}, - {'.', '.', '$'} + {'.', '#', '.'}, + {'.', '.', '.'}, + {'.', '.', '#'}, + {'.', '#', '.'}, + {'.', '.', '$'}, }; boolean[][] visited = new boolean[maze.length][maze[0].length]; -- cgit 1.4.1-2-gfad0