LeetCode——1162.地图分析(Java)

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;
//}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值