-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
49 lines (45 loc) · 1.58 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.company;
import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {
int[][] grid = new int[][]{
new int[]{1, 0, 0},
new int[]{0, 0, 0},
new int[]{0, 0, 0}
};
maxDistance(grid);
}
public static int maxDistance(int[][] grid) {
boolean[][] visited = new boolean[grid.length][grid[0].length];
ArrayDeque<int[]> queue = new ArrayDeque();
for (int x = 0; x < grid.length; x++) {
for (int y = 0; y < grid[0].length; y++) {
if (grid[x][y] == 1) {
visited[x][y] = true;
queue.offer(new int[]{x, y});
}
}
}
if(queue.isEmpty() || queue.size() == grid.length * grid[0].length) return -1;
int[] dir = {0, 1, 0, -1, 0};
int level = 0;
while (!queue.isEmpty()) {
int count = queue.size();
level++;
for (int i = 0; i < count; i++) {
int[] cur = queue.poll();
for (int j = 0; j < 4; j++) {
int newX = cur[0] + dir[j];
int newY = cur[1] + dir[j + 1];
if (newX < 0 || newX >= grid.length || newY < 0 || newY >= grid[0].length)
continue;
if(!visited[newX][newY]){
visited[newX][newY] = true;
queue.offer(new int[]{newX, newY});
}
}
}
}
return level - 1;
}
}