首页 > 其他分享 >红与黑(用dfs求洪水灌溉)

红与黑(用dfs求洪水灌溉)

时间:2022-09-02 14:14:39浏览次数:53  
标签:cnt 洪水 红与黑 sy dfs st nx int

https://www.acwing.com/problem/content/1115/

#include<bits/stdc++.h>
using namespace std;
const int N = 25;
char g[N][N];
bool st[N][N];
int n, m;
int sx, sy;
int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };

void dfs(int x, int y, int &cnt)
{
    st[x][y] = true;

    for (int i = 0; i < 4; i++)
    {
        int nx = x + dx[i], ny = y + dy[i];
        if (nx >= 1 && nx <= n&&ny >= 1 && ny <= m&&st[nx][ny]==false&&g[nx][ny]=='.')
        {
            cnt++;

            st[nx][ny] = true;
            dfs(nx, ny, cnt);
        }
    }
     return;
}

int main()
{
    while (cin >> m >> n, n || m)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                cin >> g[i][j];
                if (g[i][j] == '@')
                {
                    sx = i, sy = j;
                }
            }
        }
        int cnt = 1;
        memset(st, false, sizeof st);
        dfs(sx, sy, cnt);
        cout << cnt << endl;
    }
    return 0;
}

标签:cnt,洪水,红与黑,sy,dfs,st,nx,int
From: https://www.cnblogs.com/xjtfate/p/16649656.html

相关文章