Skip to content

Commit

Permalink
feat: add RoutePlanner
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegand committed Jun 2, 2024
1 parent 09de37c commit 510366c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
47 changes: 47 additions & 0 deletions Java/RoutePlanner.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
- [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

0 comments on commit 510366c

Please sign in to comment.