POJ2386 Lake Counting

题目描述

Due to recent rains, water has pooled in various places in Farmer John’s field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water (‘W’) or dry land (’.’). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

题目和UVA572一毛一样,就是换了个符号表示,不多说了。
日常安利本题我的博客

思路分析

我光明正大的表示,就是划水,你打我啊~~~

代码

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
#include<time.h>
using namespace std ;

#define mem(a) memset(a,0,sizeof(a))
#define ll long long

const double eps = 1e-8;
const int maxn = 110;//须填写
const int inf = 0x3f3f3f3f;

char pic[maxn][maxn];
int vis[maxn][maxn];
int n,m;

void dfs(int x, int y, int s)
{
    if(x<0||y<0||x>=m||y>=n)
        return ;
    for(int i=-1;i<2;i++)
    {
        for(int j=-1;j<2;j++)
        {
            if(vis[i+x][j+y] == 0&&pic[i+x][j+y]=='W')
            {
                vis[i+x][j+y] = s;
                dfs(i+x,j+y,s);
            }
        }
    }
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("sample.out", "w", stdout);
    int results;
    scanf("%d%d",&m,&n);

    results = 0;
    mem(vis);
    for(int i=0;i<m;i++)
    {
        scanf("%s",pic[i]);
    }
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(vis[i][j] == 0&&pic[i][j]=='W')
            {
                vis[i][j] = ++results;
                dfs(i,j,results);
            }
        }
    }
    printf("%d\n",results);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值