Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
128 changes: 116 additions & 12 deletions src/SalamanderSearch.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import java.util.ArrayList;
import java.util.List;
import java.util.*;

public class SalamanderSearch {
public static void main(String[] args) {
char[][] enclosure1 = {
{'.','.','.','.','.','.'},
{'W','.','W','W','.','.'},
{'.','.','W','.','.','W'},
{'f','W','.','.','W','.'},
{'W','.','W','s','.','.'},
{ '.', '.', '.', '.', '.', '.' },
{ 'W', '.', 'W', 'W', '.', '.' },
{ '.', '.', 'W', '.', '.', 'W' },
{ 'f', 'W', '.', '.', 'W', '.' },
{ 'W', '.', 'W', 's', '.', '.' },
};

char[][] enclosure2 = {
{'.','.','.','.','.','.'},
{'W','W','W','W','s','.'},
{'.','.','W','.','.','W'},
{'f','W','.','.','W','.'},
{'W','.','W','.','.','.'},
{ '.', '.', '.', '.', '.', '.' },
{ 'W', 'W', 'W', 'W', 's', '.' },
{ '.', '.', 'W', '.', '.', 'W' },
{ 'f', 'W', '.', '.', 'W', '.' },
{ 'W', '.', 'W', '.', '.', '.' },
};

Set<int[]> coordinateSet = new HashSet<>();
int[] coord1 = new int[] { 1, 5 };
int[] coord2 = new int[] { 3, 7 };
int[] coord3 = new int[] { 1, 5 };

coordinateSet.add(coord1);
coordinateSet.add(coord2);
coordinateSet.add(coord3);

// the actual contents are not what's been added, it's the references to each
// variable that's added!
System.out.println(coordinateSet.size());

}

/**
Expand All @@ -43,6 +56,97 @@ public static void main(String[] args) {
* @return whether the salamander can reach the food
*/
public static boolean canReach(char[][] enclosure) {
int[] start = salamanderLocation(enclosure);
boolean[][] visited = new boolean[enclosure.length][enclosure[0].length];
return canReach(enclosure, start, visited);
}

public static boolean canReach(char[][] enclosure, int[] current, boolean[][] visited) {

int curR = current[0];
int curC = current[1];

// base case already been there
if (visited[curR][curC])
return false;
// base case found food
if (enclosure[curR][curC] == 'f')
return true;

visited[curR][curC] = true;

List<int[]> moves = possibleMoves(enclosure, current);
for (int[] move : moves) {
if (canReach(enclosure, move, visited))
return true;
}
return false;
}

// int array holds {row,col} location of sal
public static int[] salamanderLocation(char[][] enclosure) {
// base case if array null auto produces NPE
for (int r = 0; r < enclosure.length; r++) {
for (int c = 0; c < enclosure[0].length; c++) {
if (enclosure[r][c] == 's') {
return new int[] { r, c };
}
}
}
throw new IllegalArgumentException("No salamander present");
}

public static List<int[]> possibleMoves(char[][] enclosure, int[] current) {
int curR = current[0];
int curC = current[1];

List<int[]> moves = new ArrayList<>();

int[][] directions = new int[][] {
{ -1, 0 },
{ 1, 0 },
{ 0, -1 },
{ 0, 1 }
};

for (int[] direction : directions) {
int newR = curR + direction[0];
int newC = curC + direction[1];
if (newR >= 0 && newR < enclosure.length &&
newC >= 0 && newC < enclosure[0].length &&
enclosure[newR][newC] != 'W') {
moves.add(new int[] { newR, newC });
}

}

// // UP
// int newR = curR - 1;
// int newC = curC;
// if(newR >= 0 && enclosure[newR][newC] != 'W') {
// moves.add(new int[]{newR, newC});
// }

// // DOWN
// newR = curR + 1;
// newC = curC;
// if(newR < enclosure.length && enclosure[newR][newC] != 'W') {
// moves.add(new int[]{newR, newC});
// }

// // LEFT
// newR = curR;
// newC = curC - 1;
// if(newC >= 0 && enclosure[newR][newC] != 'W') {
// moves.add(new int[]{newR, newC});
// }

// // RIGHT
// newR = curR;
// newC = curC + 1;
// if(newC < enclosure[0].length && enclosure[newR][newC] != 'W') {
// moves.add(new int[]{newR, newC});
// }
return moves;
}
}
Loading