poj 2386 Lake Counting

代码不写就手生啊。。。

注意输入, 二维数组

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=100+5;
char field[maxn][maxn];
int n,m;
void input()
{
   scanf("%d%d", &n, &m);
   for(int i=0;i<n;i++)
       scanf("%s", field[i]);
}

void dfs(int x, int y)
{
    field[x][y]='.';
    for(int dx=-1; dx<=1; dx++)
        for(int dy=-1; dy<=1; dy++)
        {
            int nx=x+dx,  ny=y+dy;
            if(nx<n && nx>=0 && ny<m && ny>=0 && field[nx][ny]=='W')
                dfs(nx, ny);
        }
}

int solve()
{
    int ans=0;
    for(int x=0; x<n; x++)
        for(int y=0; y<m; y++) if(field[x][y]=='W')
        {
            dfs(x, y);
            ans++;
        }
    return ans;
}

int main()
{
    input();
    printf("%d
", solve());

    return 0;
}
原文地址:https://www.cnblogs.com/cute/p/4332452.html