首页 > 其他分享 >LeetCode 200. 岛屿数量

LeetCode 200. 岛屿数量

时间:2023-07-07 12:23:29浏览次数:28  
标签:200 int 310 岛屿 st && LeetCode

class Solution {
public:
    bool st[310][310];
    int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
    int m,n;
    int numIslands(vector<vector<char>>& g) {
        int res=0;
        n=g.size(),m=g[0].size();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                if(!st[i][j]&&g[i][j]=='1')
                {
                    dfs(i,j,g);
                    res++;
                }
            }
        return res;
    }
    void dfs(int x,int y,vector<vector<char>>& g)
    {
        st[x][y]=true;
        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&&!st[a][b]&&g[a][b]=='1')
                dfs(a,b,g);
        }
    }
};

标签:200,int,310,岛屿,st,&&,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17534624.html

相关文章

  • LeetCode 169. 多数元素
    classSolution{public:intmajorityElement(vector<int>&nums){intcnt=1;intres=nums[0];for(inti=1;i<nums.size();i++){if(nums[i]==res)cnt++;elsecnt--;i......
  • [LeetCode] 2024. Maximize the Confusion of an Exam
    Ateacheriswritingatestwith n true/falsequestions,with 'T' denotingtrueand 'F' denotingfalse.Hewantstoconfusethestudentsby maximizing thenumberof consecutive questionswiththe same answer(multipletruesormultiple......
  • CentOS7母盘系统硬盘200G,通过母盘创建新系统硬盘为500G,多余未用空间扩展
    [查看现有磁盘] lsblk-l创建新磁盘①fdisk/dev/sda②命令(输入m获取帮助):n③Select(defaulte):p④命令(输入m获取帮助):t⑤Hex代码(输入L列出所有代码):8e  //已将分区“Linux”的类型更改为“LinuxLVM”⑥命令(输入m获取帮助):w [root@SkyWalking......
  • ubuntu2004 ROS1安装
    ubuntu初始环境配置ROS11.换源并更新数据库ubuntu2004换源#备份原来的源并且另存sudocp-v/etc/apt/sources.list/etc/apt/sources.list.backup#执行chmod命令更改文件权限使软件源文件可编辑sudochmod777/etc/apt/sources.list#通过gedit命令编辑软件源:sudoged......
  • 【暑假题目】20030703 两数相加
    两数相加题目请使用C++计算出2^2023与3^2023的和题目分析首先通过题目,我们将所求的两个加数看为a,b,我们可以关注到两个点:1.首先要解决的是两个加数的求法,这里分别有两种求法:①通过for循环求得a,b两个加数。②通过指数函数pow函数得到a,b两个加数。在可以调用函数的情况下......
  • S7200通过以太网模块与SMART200数据交换案例
    捷米特以太网通讯模块,型号有ETH-S7200和ETH-S7300-JM01,适用于西门子S7-200/S7-300/S7-400、SMARTS7-200、西门子数控840D、840DSL、合信、亿维PLC的PPI/MPI/PROFIBUS转以太网。用于西门子S7-200/S7-300/S7-400程序上下载、上位监控、设备联网和数据采集。支持与S7-200SMART、S7......
  • [LeetCode] 2178. Maximum Split of Positive Even Integers
    Youaregivenaninteger finalSum.Splititintoasumofa maximum numberof unique positiveevenintegers.Forexample,given finalSum=12,thefollowingsplitsare valid (uniquepositiveevenintegerssummingupto finalSum): (12), (2+10), ......
  • 西门子S7200系列PLC转以太网通讯处理器
    捷米特ETH-S7200-JM01以太网模块适用于西门子SMARTS7-200、西门子数控840D、840DSL、合信、亿维PLC的PPI/MPI/PROFIBUS转以太网,用于西门子S7-200/S7-300/S7-400程序上下载、上位监控、设备联网和数据采集。ETH-S7200典型应用:  支持与S7-200SMART、S7-1200/1500、S7-200......
  • leetcode-1629-easy
    SlowestKeyYouhaveabombtodefuse,andyourtimeisrunningout!Yourinformerwillprovideyouwithacirculararraycodeoflengthofnandakeyk.Todecryptthecode,youmustreplaceeverynumber.Allthenumbersarereplacedsimultaneously.I......
  • leetcode-1652-easy
    DefusetheBombYouhaveabombtodefuse,andyourtimeisrunningout!Yourinformerwillprovideyouwithacirculararraycodeoflengthofnandakeyk.Todecryptthecode,youmustreplaceeverynumber.Allthenumbersarereplacedsimultaneously......