• 【小米oj】 数数有几个岛


    dfs

     1 #define mm(a) memset(a,0,sizeof(a));
     2 #define max(x,y) (x)>(y)?(x):(y)
     3 #define min(x,y) (x)<(y)?(x):(y)
     4 #define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
     5 #define rep(i,a,b) for(int i=(a);i<=(b);i++)
     6 #define per(i,b,a) for(int i=(b);i>=(a);i--)
     7 #include<bits/stdc++.h>
     8 typedef long long ll;
     9 #define PII pair<ll,ll>
    10 using namespace std;
    11 const int INF=0x3f3f3f3f;
    12 const int MAXN=(int)2e5 + 5;
    13 const ll mod=1e9+7;
    14 
    15 
    16 char s[1005][1005],a[MAXN];
    17 int n,vis[1005][1005],m;
    18 void dfs(int i,int j) {
    19     if(i<1||i>n||j<1||j>m)return;
    20     if(s[i][j]=='0')return;
    21     s[i][j]='0';
    22     dfs(i,j+1);
    23     dfs(i+1,j);
    24     dfs(i-1,j);
    25     dfs(i,j-1);
    26 }
    27 int main() {
    28     while(~scanf("%s",a+1)) {
    29         m=strlen(a+1)/2+1;
    30         n++;
    31         for(int i=1; i<=m; i++)s[n][i]=a[i*2-1];
    32 //
    33     }
    34     int ans=0;
    35 //    for(int i=1; i<=n; i++)printf("%s
    ",s[i]+1);
    36     for(int i=1; i<=n; i++)for(int j=1; j<=m; j++)if(s[i][j]=='1')dfs(i,j),ans++;
    37     printf("%d
    ",ans);
    38     return 0;
    39 }
  • 相关阅读:
    4.23上机练习
    4.17java作业
    4.16java作业
    leetcode 189
    leetcode 172
    leetcode 171
    leetcode 169
    win10内网外网智能访问
    leetcode 168
    leetcode 165
  • 原文地址:https://www.cnblogs.com/dogenya/p/10815619.html
Copyright © 2020-2023  润新知