首页 > 其他分享 >PTA L2-048 寻宝图

PTA L2-048 寻宝图

时间:2023-10-19 21:11:48浏览次数:36  
标签:int 岛屿 PTA ++ length grid dfs 048 L2

目录

PTA L2-048 寻宝图 Flood Fill算法

此题要求出岛屿数量和有宝藏的岛屿数量, 搜索就行

先看前置题目: leetcode 200. 岛屿数量

给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。

岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

示例 1:

输入:grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出:1

示例 2:

输入:grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出:3

提示:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] 的值为 '0''1'

​ 统计岛屿数量 flood fill

class Solution {
    int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
    int res = 0;

    void dfs(char[][] grid, int x, int y) {
        grid[x][y] = '0';
        for (int i = 0; i < 4; i ++ ) {
            int a = x + dx[i], b = y + dy[i];
            if (a >= 0 && a < grid.length && b >= 0 && b < grid[0].length && grid[a][b] == '1')
                dfs(grid, a, b);
        }
    }

    public int numIslands(char[][] grid) {
        int n = grid.length;
        int m = grid[0].length;
        for (int i = 0; i < n; i ++ ) {
            for (int j = 0; j < m; j ++ ) {
                if (grid[i][j] == '1') {
                    dfs(grid, i, j);
                    res += 1;
                }
            }
        }   
        return res; 
    }
}

再看此题解(深搜)

import java.io.*;

public class Main {
    static int islands, treasures, n, m;
    static int[] dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
    static boolean flag; // flag 判断岛上有没有宝藏

    static void dfs(char[][] grid, int x, int y) {
        if (grid[x][y] != '1') flag = true;
        grid[x][y] = '0';

        for (int i = 0; i < 4; i ++ ) {
            int a = x + dx[i], b = y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= m) continue;
            if (grid[a][b] != '0') dfs(grid, a, b);
        }
    }
    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        
        char[][] grid = new char[n][m];
        for (int i = 0; i < n; i ++ ) {
            String str = br.readLine();
            grid[i] = str.toCharArray();
        }
    
        for (int i = 0; i < n; i ++ ) 
            for (int j = 0; j < m; j ++ ) {
                flag = false;
                if (grid[i][j] != '0') {
                    islands++;
                    dfs(grid, i, j);
                    if (flag) treasures++;
                }
            }

        System.out.println(islands + " " + treasures);
    }
}

相关题目

leetcode 463.岛屿的周长

leetcode 695.岛屿的最大面积

标签:int,岛屿,PTA,++,length,grid,dfs,048,L2
From: https://www.cnblogs.com/rimliuhan/p/17775653.html

相关文章

  • iptables常用命令
    iptables是用于配置Linux系统中的防火墙规则的命令行工具。其命令格式和常用参数的意思如下:iptables[选项]<链名><规则规范>常用选项:-A:添加规则到指定链的末尾。-D:从指定链中删除规则。-I:插入规则到指定链的开头。-L:列出指定链的规则。-F:清除指定链中的所有规则。-P:......
  • vim+ptags.py 实现跳转
    转载:https://cloud.tencent.com/developer/article/1656073网上很多帖子都是说通过ctags或者ExuberantCtags来实现函数跳转,如果你是C语言开发者,无可厚非,Python怎么办?快来看下面操作吧!1.步骤1.下载一个文件2.使用下载的文件为项目生成tags文件(里面记录了所有函数、类......
  • vue2和vue3导出页面为PDF格式:jspdf和html2canvas
    一、vue2导出PDF使用步骤1、安装html2canvas,将页面html转换成图片npminstall--savehtml2canvas卸载:npmuninstallhtml2canvas指定版本安装:[email protected]、安装jspdf,将图片生成pdfnpminstalljspdf--save3、定义全局函数在指......
  • PTA 函数与递归部分题目讲解及思路
    7-1判断素数题目分析题目输入n个数,判断其是否为质数对于判断质数,只需要满足从2开始遍历的每一个数一直到√n均无法被n整除即可关于为什么只要到√n呢?因为n=√n*√n,因此其最大的因数不会超过√n,因此可以优化减少不必要的循环ACCode#include<iostream>#include<c......
  • kubeadm init 报错ERROR FileContent--proc-sys-net-bridge-bridge-nf-call-iptables
    现象:[ERRORFileContent--proc-sys-net-bridge-bridge-nf-call-iptables]:/proc/sys/net/bridge/bridge-nf-call-iptablescontentsarenotsetto1原因:  /proc/sys/net/bridge/bridge-nf-call-iptables 文件的内容并没有设置为1解决方案echo"1">/proc/sys/net/br......
  • iptables 正常用法
    #!/bin/baship1=${group_host1}ip2=${group_host2}ip3=${group_host3}ip4=${group_host4}ip5=${group_host5}iptables-F#清空所有的防火墙规则iptables-X#删除用户自定义的空链iptables-Z#清空计数iptables-AINPUT-ptcp--dport22-jACCEPTiptables-AIN......
  • 麒麟v10 SP3上的19c rac,optachauto安装补丁出错
    1、麒麟V10SP3上新安装的一套19cRAC,在使用opatchauto打补丁时报错,具体信息如下所示。[root@db01soft]#/u01/app/19.3.0/grid/OPatch/opatchautoapply/soft/35037840/ OPatchautosessionisinitiatedatTue0ct1011:04:452023 Systeminitializationlogfi......
  • html2canvas 截图不全问题解决
    有个低代码平台项目,需求是要将canvas画布上的echarts图表等组件截图保存,如果是正常比例(也就是百分百比例)截图是正常的,但如果画布处于缩放状态进行截图的话就会因组件上的一些样式影响而导致截图不全。为了解决这一问题,在网上也查找了很多资料,终于找到解决办法,亲测有效。话不多说,......
  • 统信操作系统UOS1060上通过Fail2Ban来Ban IP
    原文链接:统信UOS1060上通过Fail2Ban来BanIPhello,大家好啊,今天给大家带来一篇在统信UOS1060上安装Fail2Ban并且当ip被ban后通过邮件发送通知的文章。Fail2Ban 是一个用于防止暴力attack的开源软件。它可以扫描日志文件(例如,SSH或Web服务器日志文件)以查找IP地址,这些IP地址在定义的......
  • Using Docker Desktop with WSL2
    WindowsSubsystemforLinux(WSL)2isafullLinuxkernelbuiltbyMicrosoft,whichletsLinuxdistributionsrunwithoutmanagingvirtualmachines.WithDockerDesktiprunningonWSL2,userscanleverageLinuxworkspacesandavoidmaintainingbothLinux......