zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    HTTP的KeepAlive是开启还是关闭?
    JS中关于in运算符的问题
    关于jQuery的inArray 方法介绍
    JS中括号的用法
    关于js中for in的缺陷浅析
    Ajax datatype:'JSON'的error问题Status1:200,JSON格式
    windows 如何查看端口占用情况?
    确认过眼神,看清 HTTP 协议
    高考完?入门级的开源项目带你开启编程之旅
    MongoDB入门系列(四):权限管理
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10591523.html
Copyright © 2011-2022 走看看