package LeetCode.OneThousandMore;
import java.util.ArrayDeque;
import java.util.Queue;
public class OneThousandAndOneHundredSixtyTwo {
public int maxDistance(int[][] grid) {
//这道题的主要思路是:
//
//一开始,我们找出所有陆地格子,将它们放入队列,作为第 0 层的结点。
//然后进行 BFS 遍历,每个结点的相邻结点可能是上、下、左、右四个方向的结点,注意判断结点位于网格边界的特殊情况。
//当遍历结束时,当前的遍历层数就是海洋格子到陆地格子的最远距离。
//注意:为了在遍历中不重复访问海洋格子,我们将已经遍历过的海洋格子的值改为 2,和原来海洋格子里的 0 区别开来。
int len = grid.length;
Queue<int[]> queue = new ArrayDeque<>();
//将所有的陆地装进队列
for (int i = 0; i <len ;i++)
for (int j = 0; j < len; j++)
if (grid[i][j] == 1)
queue.add(new int[]{i, j});
//当queue为空的时候直接返回-1
if (queue.isEmpty() || queue.size() == len * len) return -1;
//进行BFS遍历
int distance = -1;
while (!queue.isEmpty()){
distance++;
int n = queue.size();
//因为可能是多源广度优先搜索,所以将其全部遍历出来
for (int i = 0; i < n; i++){
int temp [] = queue.poll();
int r = temp[0];
int c = temp[1];
//进行四个方向的遍历
if (r - 1 >= 0 && grid[r - 1][c] == 0){
grid[r - 1][c] = 2;
queue.add(new int[]{r - 1, c});
}
if (r + 1 < len && grid[r + 1][c] == 0){
grid[r + 1][c] = 2;
queue.add(new int[]{r + 1, c});
}
if (c - 1 >= 0 && grid[r][c - 1] == 0){
grid[r][c - 1] = 2;
queue.add(new int[]{r, c - 1});
}
if (c + 1 > len && grid[r][c + 1] == 0){
grid[r][c + 1] = 2;
queue.add(new int[]{r, c + 1});
}
}
}
return distance;
}
//public int maxDistance(int[][] grid) {
// int N = grid.length;
//
// Queue<int[]> queue = new ArrayDeque<>();
// // 将所有的陆地格子加入队列
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// if (grid[i][j] == 1) {
// queue.add(new int[]{i, j});
// }
// }
// }
// // 如果我们的地图上只有陆地或者海洋,请返回 -1。
// if (queue.isEmpty() || queue.size() == N * N) {
// return -1;
// }
//
// int distance = -1;
// while (!queue.isEmpty()) {
// distance++;
// int n = queue.size();
// // 这里一口气取出 n 个结点,以实现层序遍历
// for (int i = 0; i < n; i++) {
// int[] cell = queue.poll();
// int r = cell[0];
// int c = cell[1];
// // 遍历上方单元格
// if (r-1 >= 0 && grid[r-1][c] == 0) {
// grid[r-1][c] = 2;
// queue.add(new int[]{r-1, c});
// }
// // 遍历下方单元格
// if (r+1 < N && grid[r+1][c] == 0) {
// grid[r+1][c] = 2;
// queue.add(new int[]{r+1, c});
// }
// // 遍历左边单元格
// if (c-1 >= 0 && grid[r][c-1] == 0) {
// grid[r][c-1] = 2;
// queue.add(new int[]{r, c-1});
// }
// // 遍历右边单元格
// if (c+1 < N && grid[r][c+1] == 0) {
// grid[r][c+1] = 2;
// queue.add(new int[]{r, c+1});
// }
// }
// }
//
// return distance;
//}
}
LeetCode——1162.地图分析(Java)
最新推荐文章于 2020-11-20 15:39:03 发布