洛谷 P1596 [USACO10OCT]Lake Counting S

洛谷 P1596 [USACO10OCT]Lake Counting S

https://www.luogu.com.cn/problem/P1596
很水的一题,我比较菜,不能很快的写出来。

每次遇到W的时候,就搜索一下,把搜到的W全部变成" . ",一共搜索了几次,就是有多少的水坑。

#include <bits/stdc++.h>
using namespace std;

const int N = 110;

char g[N][N];
int n, m;
bool vis[N][N];
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int res = 0;

void dfs(int x, int y)
{
    g[x][y] = '.';
    for (int i = 0; i < 8 ; i ++ )
    {
        int tx = dx[i] + x, ty = dy[i] + y;
        if (tx < 0 || tx > n || ty < 0 || ty > m) continue;
        if (g[tx][ty] == 'W')
        {
            dfs(tx, ty);
        }
    }
    return;
}

int main()
{
    cin >> n >> m;
    for (int i = 0; i < n ; i ++ )
    {
        for (int j = 0; j < m ; j ++ )
        {
            cin >> g[i][j];
        }
    }
    for (int i = 0; i < n ; i ++ )
    {
        for (int j = 0; j < m ; j ++ )
        {
            if (g[i][j] == 'W')
            {
                dfs(i, j);
                res ++ ;
            }
        }
    }
    cout << res << endl;
    return 0;
}

继续学啊,这都不会,完了呀!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值