Skip to content

Instantly share code, notes, and snippets.

@ZachOrr
Created September 19, 2012 18:03
Show Gist options
  • Select an option

  • Save ZachOrr/3751159 to your computer and use it in GitHub Desktop.

Select an option

Save ZachOrr/3751159 to your computer and use it in GitHub Desktop.
public char[][] findPath(int startRow, int startCol, int destRow, int destCol, int threshold) {
char[][] map = generateMap(threshold);
if(checkIfPassable(startRow, startCol, threshold)) {
if(startRow == destRow && startCol == destCol) {
map[destRow][destCol] = 'O';
return map;
}
else {
ArrayList<int[]> pathzor = new ArrayList<int[]>();
pathzor = findEdges(startRow, startCol, destRow, destCol, threshold, pathzor);
for (int i = 0; i < pathzor.size(); i++) {
int[] e = pathzor.get(i);
map[e[0]][e[1]] = 'O';
}
}
}
return map;
}
private ArrayList<int[]> findEdges(int row, int col, int destRow, int destCol, int threshold, ArrayList<int[]> path) {
int[] position = {row, col};
path.add(position);
if(row == destRow && col == destCol)
return path;
int[][] edges = {{row+1, col}, {row-1, col}, {row, col+1}, {row, col-1}};
for(int[] e : edges) {
if(e[0] >= 0 && e[1] >= 0)
if(checkIfPassable(e[0], e[1], threshold))
findEdges(e[0], e[1], destRow, destCol, threshold, path);
}
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment