个人赛-13

2022/6/17 23:27:07

本文主要是介绍个人赛-13,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

islands

dfs

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=55;
char a[N][N];
int n,m,res;
int dx[4]= {-1,0,1,0};
int dy[4]= {0,1,0,-1};
bool judge(int x,int y)
{
    if (x<=0||x>n||y<=0||y>m||a[x][y]=='W')
        return 0;
    return 1;
}
void dfs(int x,int y)
{
    for (int i=0; i<4; i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if (judge(xx,yy))
        {
            a[xx][yy]='W';
            dfs(xx,yy);
        }
    }
}
signed main()
{
    cin>>n>>m;
    for (int i=1; i<=n; i++)
        scanf("%s",a[i]+1);
    for (int i=1; i<=n; i++)
        for (int j=1; j<=m; j++)
            if (a[i][j]=='L')
                res++,dfs(i,j);
    printf("%d\n",res);
    return 0;
}

 



这篇关于个人赛-13的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程