2
public int maxAreaOfIsland(int[][] grid) {4
final int rows = grid.length;5
final int cols = grid[0].length;6
final int[][] dirrections = new int[][] {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};7
Map<String, List<int[]>> adj = new HashMap<>();8
boolean[][] visited = new boolean[rows][cols];9
Queue<String> queue = new LinkedList<>();12
for (int i = 0; i < grid.length; i++) {13
for (int j = 0; j < grid[i].length; j++) {15
List<int[]> list = new ArrayList<>();16
for (int[] dirrection : dirrections) {17
int newRow = dirrection[0] + i;18
int newCol = dirrection[1] + j;20
boolean isInBoard = newRow >= rows || newRow < 0 || newCol >= cols || newCol < 0;22
list.add(new int[] {newRow, newCol, grid[newRow][newCol]});26
adj.put(getNodeStringFormat(i, j, grid[i][j]), list);30
for (int i = 0; i < rows; i++) {31
for (int j = 0; j < cols; j++) {33
if (visited[i][j]) continue;34
queue.add(getNodeStringFormat(i, j, grid[i][j]));35
while (!queue.isEmpty()) {36
String currentStr = queue.poll();37
String[] current = currentStr.split(",");39
int row = Integer.valueOf(current[0]);40
int col = Integer.valueOf(current[1]);41
int isLand = Integer.valueOf(current[2]);42
if (!adj.containsKey(currentStr)) continue;43
if (visited[row][col]) continue;44
if (isLand == 1) count++;45
visited[row][col] = true;46
for (int[] item : adj.get(currentStr)) {49
int newIsLand = item[2];50
if (!visited[newRow][newCol] && newIsLand == 1 && isLand == 1)51
queue.add(getNodeStringFormat(newRow, newCol, newIsLand));54
res = Math.max(res, count);61
private String getNodeStringFormat(int row, int col, int isLand) {62
StringBuilder sb = new StringBuilder();