HDU-1241-Oil Deposits

链接:https://vjudge.net/problem/HDU-1241#author=prayerhgq

题意:

GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石油的小方格相邻,那么他们被认为是同一油藏的一部分。在这块矩形区域,可能有很多油藏。你的任务是确定有多少不同的油藏。

思路:

bfs将联通区换为*,挨个找即可。

代码:

#include <iostream>
#include <memory.h>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <queue>
#include <string>

using namespace std;

typedef long long LL;

const int MAXN = 100 + 10;

int Next[8][2] = {{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
char Map[MAXN][MAXN];
int n, m;

void BFS(int x, int y)
{
    queue<pair<int, int> > que;
    que.emplace(x, y);
    while (!que.empty())
    {
        for (int i = 0;i < 8;i++)
        {
            int tx = que.front().first + Next[i][0];
            int ty = que.front().second + Next[i][1];
            if (tx < 1 || tx > n || ty < 1 || ty > m)
                continue;
            if (Map[tx][ty] == '*')
                continue;
            que.emplace(tx, ty);
            Map[tx][ty] = '*';
        }
        que.pop();
    }
}

int main()
{
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
            break;
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
                cin >> Map[i][j];
        int res = 0;
        for (int i = 1;i <= n;i++)
            for (int j = 1;j <= m;j++)
            {
                if (Map[i][j] == '@')
                    res++, BFS(i, j);
            }
        cout << res << endl;
    }

    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/YDDDD/p/10591523.html