代码随想录——图论一刷day02

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣695. 岛屿的最大面积
  • 二、力扣1020. 飞地的数量
  • 三、力扣1254. 统计封闭岛屿的数目


前言

`


一、力扣695. 岛屿的最大面积

淹没岛屿的递归

java">class Solution {
    int[][] move = {
            {0,1},
            {0,-1},
            {-1,0},
            {1,0}
    };
    int count = 0;
    public int maxAreaOfIsland(int[][] grid) {
        
        int res = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == 1){
                    count = 1;
                    dfs(grid, i , j);
                    res = Math.max(count, res);
                }
            }
        }
        return res;
    }
    public void dfs(int[][] grid, int x, int y){
        grid[x][y] = 0;
        for(int i = 0; i < 4; i ++){
            int nextX = x + move[i][0];
            int nextY = y + move[i][1];
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length ||             grid[nextX][nextY] == 0){
                continue;
            }
            count ++;
            dfs(grid, nextX, nextY);
        }
    }
}

使用标记数组的递归

java">class Solution {
    int[][] move = {
            {0,1},
            {0,-1},
            {-1,0},
            {1,0}
    };
    boolean[][] flag;
    int count = 0;
    public int maxAreaOfIsland(int[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        int res = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == 1 && flag[i][j] == false){
                    count = 1;
                    dfs(grid, i , j);
                    res = Math.max(count, res);
                }
            }
        }
        return res;
    }
    public void dfs(int[][] grid, int x, int y){
        flag[x][y] = true;
        for(int i = 0; i < 4; i ++){
            int nextX = x + move[i][0];
            int nextY = y + move[i][1];
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length ||             grid[nextX][nextY] == 0 || flag[nextX][nextY] == true){
                continue;
            }
            count ++;
            dfs(grid, nextX, nextY);
        }
    }
}

广度优先搜索

java">class Solution {
    int[][] move = {
        {0,1},
        {0,-1},
        {-1,0},
        {1,0}
    };
    int count = 0;
    boolean[][] flag;
    public int maxAreaOfIsland(int[][] grid) {
        int res = 0;
        flag = new boolean[grid.length][grid[0].length];
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == 1 && flag[i][j] == false){
                    count = 1;
                    bfs(grid, i, j);
                    res = Math.max(res, count);
                }
            }
        }
        return res;
    }
    public void bfs(int[][] grid, int x, int y){
        Deque<int[]> deq = new LinkedList<>();
        flag[x][y] = true;
        deq.offerLast(new int[]{x, y});
        while(!deq.isEmpty()){
            int[] cur = deq.pollFirst();
            for(int i = 0; i < 4; i ++){
                int nextX = cur[0] + move[i][0];
                int nextY = cur[1] + move[i][1];
                if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length || grid[nextX][nextY] == 0 || flag[nextX][nextY] == true){
                    continue;
                }
                count ++;
                flag[nextX][nextY] = true;
                deq.offerLast(new int[]{nextX, nextY});
            }
        }
    }
}

二、力扣1020. 飞地的数量

DFS淹没岛屿同时一边统计边界岛屿的陆地面积, 一边统计总陆地面积,最后返回差值

java">class Solution {
    int count = 0;
    int[][] move = {
        {0,1},
        {0,-1},
        {-1,0},
        {1,0}
    };
    boolean tag = false;
    public int numEnclaves(int[][] grid) {
        int res = 0;
        int edge = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == 1){
                    count = 1;
                    dfs(grid, i, j);
                    if(tag == true){
                        edge += count;
                        tag = false;
                    }
                    res += count;
                }
            }
        }
        return res - edge;
    }
    public void dfs(int[][] grid, int x, int y){
        grid[x][y] = 0;
        for(int i = 0; i < 4; i ++){
            int nextX = move[i][0] + x;
            int nextY = move[i][1] + y;
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length){
                tag = true;
            }
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length || grid[nextX][nextY] == 0){
                continue;
            }
            count ++;
            dfs(grid, nextX, nextY);
        }
    }
}

DFS使用标记数组同时一边统计边界岛屿的陆地面积, 一边统计总陆地面积,最后返回差值

java">class Solution {
    int count = 0;
    int[][] move = {
        {0,1},
        {0,-1},
        {-1,0},
        {1,0}
    };
    boolean[][] flag;
    boolean tag = false;
    public int numEnclaves(int[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        int res = 0;
        int edge = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == 1 && flag[i][j] == false){
                    count = 1;
                    dfs(grid, i, j);
                    if(tag == true){
                        edge += count;
                        tag = false;
                    }
                    res += count;
                }
            }
        }
        return res - edge;
    }
    public void dfs(int[][] grid, int x, int y){
        flag[x][y] = true;
        for(int i = 0; i < 4; i ++){
            int nextX = move[i][0] + x;
            int nextY = move[i][1] + y;
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length){
                tag = true;
            }
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length || grid[nextX][nextY] == 0 || flag[nextX][nextY] == true){
                continue;
            }
            count ++;
            dfs(grid, nextX, nextY);
        }
    }
}

BFS使用标记数组同时一边统计边界岛屿的陆地面积, 一边统计总陆地面积,最后返回差值

java">class Solution {
    int count = 0;
    int[][] move = {
        {0,1},
        {0,-1},
        {-1,0},
        {1,0}
    };
    boolean[][] flag;
    boolean tag = false;
    public int numEnclaves(int[][] grid) {
        flag = new boolean[grid.length][grid[0].length];
        int res = 0;
        int edge = 0;
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[i].length; j ++){
                if(grid[i][j] == 1 && flag[i][j] == false){
                    count = 1;
                    bfs(grid, i, j);
                    if(tag == true){
                        edge += count;
                        tag = false;
                    }
                    res += count;
                }
            }
        }
        return res - edge;
    }
    public void bfs(int[][] grid, int x, int y){
        flag[x][y] = true;
        Deque<int[]> deq = new LinkedList<>();
        deq.offerLast(new int[]{x,y});
        while(!deq.isEmpty()){
            int[] cur = deq.pollFirst();
            for(int i = 0; i < 4; i ++){
                int nextX = move[i][0] + cur[0];
                int nextY = move[i][1] + cur[1];
                if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length){
                    tag = true;
                }
                if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length || grid[nextX][nextY] == 0 || flag[nextX][nextY] == true){
                    continue;
                }
                count ++;
                flag[nextX][nextY] = true;
                deq.offerLast(new int[]{nextX,nextY});
            }
        } 
    }
}

三、力扣1254. 统计封闭岛屿的数目

java">class Solution {
    int[][] move = {
        {1,0},
        {-1,0},
        {0,1},
        {0,-1}
    };
    public int closedIsland(int[][] grid) {
        int res = 0;
        for(int i = 0; i < grid.length; i ++){
            if(grid[i][0] == 0){
                dfs(grid, i, 0);
            }
            if(grid[i][grid[0].length-1] == 0){
                dfs(grid, i, grid[0].length-1);
            }
        }
        for(int i = 0; i < grid[0].length; i ++){
            if(grid[0][i] == 0){
                dfs(grid, 0, i);
            }
            if(grid[grid.length-1][i] == 0){
                dfs(grid, grid.length-1, i);
            }
        }
        for(int i = 0; i < grid.length; i ++){
            for(int j = 0; j < grid[0].length; j ++){
                if(grid[i][j] == 0){
                    res ++;
                    dfs(grid, i, j);
                }
            }
        }
        return res;
    }
    public void dfs(int[][] grid, int x, int y){
        grid[x][y] = 1;
        for(int i = 0; i < 4; i ++){
            int nextX = move[i][0] + x;
            int nextY = move[i][1] + y;
            if(nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[x].length || grid[nextX][nextY] == 1){
                continue;
            }
            dfs(grid, nextX, nextY);
        }
    }
}

http://www.niftyadmin.cn/n/5090143.html

相关文章

gRPC之gRPC Gateway

1、gRPC Gateway etcd3 API全面升级为gRPC后&#xff0c;同时要提供REST API服务&#xff0c;维护两个版本的服务显然不太合理&#xff0c;所以 grpc-gateway 诞生了。通过protobuf的自定义option实现了一个网关&#xff0c;服务端同时开启gRPC和HTTP服务&#xff0c; HTTP服…

Spring Cloud--Nacos+@RefreshScope实现配置的动态更新

原文网址&#xff1a;Spring Cloud--NacosRefreshScope实现配置的动态更新_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍SpringCloud整合Nacos使用RefreshScope实现动态更新配置。 官网 Nacos Spring Cloud 快速开始 动态更新的介绍 动态更新的含义&#xff1a;修改应…

不解压的情况下从各种压缩包中直接读取文本行内容

文章目录 &#xff08;零&#xff09;前言&#xff08;一&#xff09;【ZIP】格式&#xff08;1.1&#xff09;Python&#xff08;1.2&#xff09;Java&#xff08;1.3&#xff09;Golang&#xff08;1.4&#xff09;Pascal &#xff08;二&#xff09;【GZIP】格式&#xff08…

VR太空舱体验馆VR神舟返回舱VR虚拟现实科技科普乐园

VR航天航空设备&#xff0c;寓教于乐 VR科技正成为航天航空领域的新宠。作为一种沉浸式的数字技术&#xff0c;VR(Virtual Reality&#xff0c;虚拟现实)能够为用户创造出逼真的虚拟环境&#xff0c;让人们仿佛身临其境。借助VR技术&#xff0c;我们可以带领学生和游客深入了解…

uniapp +vue3 练习 首页页面展示 我的页面展示 登录展示 拨打固定的电话 页面跳转

uniapp拨打固定的电话 function Hotline() {// 拨打电话uni.makePhoneCall({phoneNumber: 19969547693})}页面跳转 //普通跳转function homepage() {uni.navigateTo({url: /pages/homepage/homepage});}//二、uni.redirectTo关闭当前页面&#xff0c;跳转到应用内的某个页面。…

TOR(Top of Rack)

TOR TOR&#xff08;Top of Rack&#xff09;指的是在每个服务器机柜上部署1&#xff5e;2台交换机&#xff0c;服务器直接接入到本机柜的交换机上&#xff0c;实现服务器与交换机在机柜内的互联。虽然从字面上看&#xff0c;Top of Rack指的是“机柜顶部”&#xff0c;但实际T…

Linux- 自定义一个ARP请求

自定义一个ARP请求或响应&#xff0c;并使用AF_PACKET套接字发送&#xff0c;需要手动创建整个以太网帧。 下面是一个简单的C代码示例&#xff0c;用于发送一个ARP请求&#xff0c;查询给定IP地址的MAC地址&#xff1a; #include <stdio.h> #include <stdlib.h> …

带你解密【微信小程序】如何入门

&#x1f3c5;我是默&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;在这里&#xff0c;我要推荐给大家我的专栏《微信小程序》。&#x1f3af;&#x1f3af; &#x1f680;无论你是编程小白&#xff0c;还是有一定基础的程序员&#xff0c;这…