diff --git a/Java/RoutePlanner.java b/Java/RoutePlanner.java new file mode 100644 index 0000000..92c5ece --- /dev/null +++ b/Java/RoutePlanner.java @@ -0,0 +1,47 @@ +import java.util.*; + +public class RoutePlanner { + + public static boolean routeExists(int fromRow, int fromColumn, int toRow, int toColumn, boolean[][] mapMatrix) { + boolean[][] visited = new boolean[mapMatrix.length][mapMatrix[0].length]; + return dfs(fromRow, fromColumn, toRow, toColumn, mapMatrix, visited); + } + + private static boolean dfs(int row, int col, int toRow, int toCol, boolean[][] mapMatrix, boolean[][] visited) { + if (row < 0 || + col < 0 || + row >= mapMatrix.length || + col >= mapMatrix[0].length || + !mapMatrix[row][col] || + visited[row][col] + ) { + return false; + } + + if (row == toRow && col == toCol) { + return true; + } + + visited[row][col] = true; + + if (dfs(row + 1, col, toRow, toCol, mapMatrix, visited) || + dfs(row - 1, col, toRow, toCol, mapMatrix, visited) || + dfs(row, col + 1, toRow, toCol, mapMatrix, visited) || + dfs(row, col - 1, toRow, toCol, mapMatrix, visited) + ) { + return true; + } + + return false; + } + + public static void main(String[] args) { + boolean[][] mapMatrix = { + { true, false, false }, + { true, true, false }, + { false, true, true } + }; + + System.out.println(routeExists(0, 0, 2, 2, mapMatrix)); + } +} diff --git a/README.md b/README.md index 33b199f..2e5dcc5 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,6 @@ [Testdome](https://www.testdome.com/tests/java-online-test/24) -## Continued Development - -- Java / Route Planner - ## Useful Resources - [Blog](https://javahungry.blogspot.com/2020/06/add-char-to-string.html) - add char to string @@ -51,4 +47,8 @@ - [Stack Overflow](https://stackoverflow.com/questions/28417642/whats-the-best-way-to-make-the-driver-wait-until-a-condition-is-true-and-contin) - best way to make driver wait until a condition is true - [Geeks for Geeks](https://www.geeksforgeeks.org/array-copy-in-java/) - array copy in java - [Stack Overflow](https://stackoverflow.com/questions/27857011/how-to-split-a-string-array-into-small-chunk-arrays-in-java) - chunk an array -- [Techie Delight](https://www.techiedelight.com/check-index-exists-array-java/) - check index exists array java \ No newline at end of file +- [Techie Delight](https://www.techiedelight.com/check-index-exists-array-java/) - check index exists array java +- [Baeldung](https://www.baeldung.com/java-depth-first-search) - java depth first search +- [FavTutor](https://favtutor.com/blogs/depth-first-search-java) - depth first search java +- [Programiz](https://www.programiz.com/dsa/graph-dfs) - graph dfs +- [Java Challengers](https://javachallengers.com/depth-first-search-with-java/) - depth first search with java \ No newline at end of file