HDU - 1241 Oil Deposits

题目链接:

  https://vjudge.ppsucxtt.cn/problem/HDU-1241

思路:

  深搜找连通图数即可 

代码:

#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
#include <cmath>
#define fastio ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

const int N = 1010;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 998244353;

int dis[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};

char a[110][110];
bool vis[110][110];
int n, m, ans;

bool check(int i, int j)
{
    if (i >= 1 && i <= n && j >= 1 && j <= m && a[i][j] != '*' && !vis[i][j])
        return true;
    return false;
}

void dfs(int x, int y)
{
    vis[x][y] = true;
    for (int k = 0; k < 8; k++)
    {
        int i = x + dis[k][0], j = y + dis[k][1];
        if (check(i, j))
            dfs(i, j);
    }
}

int main()
{
    fastio;
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
            break;
        memset(vis, false, sizeof vis);
        ans = 0;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                cin >> a[i][j];
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                if (!vis[i][j] && a[i][j] != '*')
                    dfs(i, j), ans++;
        cout << ans << endl;
    }

    return 0;
}

 

上一篇:hdu 1078


下一篇:2021百度之星初赛 A迷失(DP+flody+二进制优化)