Last active
September 26, 2020 04:51
-
-
Save magicoder10/c35d83ca2d7155625184c9f91f06166f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
/** | |
* @param side_length: the length of a side of the lake (it's a square) | |
* @param lake_grid: a 2D matrix representing the lake 0= ice, 1= snowbank, -1= hole | |
* @param albert_row: row of Albert's snowbank | |
* @param albert_column: column of Albert's snowbank | |
* @param kuna_row: row of Kuna's snowbank | |
* @param kuna_column: column of Kuna's snowbank | |
* @return: a bool - whether Albert can escape | |
*/ | |
private static final int ICE = 0; | |
private static final int SNOW_BANK = 1; | |
private static final int HOLE = -1; | |
private int[][] DIRECTIONS = new int[][]{ | |
{-1, 0}, | |
{0, -1}, | |
{0, 1}, | |
{1, 0} | |
}; | |
public boolean lakeEscape(int side_length, int[][] lake_grid, int albert_row, int albert_column, int kuna_row, int kuna_column) { | |
// write your code here | |
if (side_length < 1 || lake_grid == null || albert_row < 0 || albert_column >= side_length || kuna_row < 0 || kuna_column >= side_length) { | |
return false; | |
} | |
Queue<int[]> queue = new LinkedList<>(); | |
boolean[][] visited = new boolean[side_length][side_length]; | |
queue.offer(new int[]{albert_row, albert_column}); | |
visited[albert_row][albert_column] = true; | |
while (!queue.isEmpty()) { | |
int[] point = queue.poll(); | |
int row = point[0]; | |
int col = point[1]; | |
for (int[] direction : DIRECTIONS) { | |
int deltaRow = direction[0]; | |
int deltaCol = direction[1]; | |
int[] nextPoint = getNextPoint(lake_grid, side_length, row, col, deltaRow, deltaCol); | |
int nextRow = nextPoint[0]; | |
int nextCol = nextPoint[1]; | |
if (nextRow == -1 && nextCol == -1) { | |
continue; | |
} | |
if (visited[nextRow][nextCol]) { | |
continue; | |
} | |
visited[nextRow][nextCol] = true; | |
if (lake_grid[nextRow][nextCol] == SNOW_BANK) { | |
queue.offer(new int[]{nextRow, nextCol}); | |
} | |
} | |
} | |
if (!canReachShore(visited, side_length)) { | |
return false; | |
} | |
return canSaveDog(visited, kuna_row, kuna_column); | |
} | |
private int[] getNextPoint(int[][] lakeGrid, int sideLength, int row, int col, int deltaRow, int deltaCol) { | |
while (isValid(sideLength, row + deltaRow, col + deltaCol)) { | |
row += deltaRow; | |
col += deltaCol; | |
switch(lakeGrid[row][col]) { | |
case ICE: | |
continue; | |
case HOLE: | |
return new int[]{-1, -1}; | |
default: | |
return new int[]{row, col}; | |
} | |
} | |
return new int[]{row, col}; | |
} | |
private boolean isValid(int sideLength, int row, int col) { | |
return row >= 0 && row < sideLength && col >= 0 && col < sideLength; | |
} | |
private boolean canReachShore(boolean[][] visited, int sideLength) { | |
for (int i = 0; i < sideLength; i++) { | |
if (visited[0][i] || visited[sideLength - 1][i]) { | |
return true; | |
} | |
if (visited[i][0] || visited[i][sideLength - 1]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private boolean canSaveDog(boolean[][] visited, int dogRow, int dogCol) { | |
return visited[dogRow][dogCol]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment