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;
    }
    

      

  • 相关阅读:
    转 UICollectionView 详解
    springboot配置ssl证书
    服务器ganglia安装(带有登录验证)
    eureka配置说明
    Servlet中获取请求参数问题
    apidoc学习(接口文档定义取代word)
    markdown语法
    JVM分析
    ftp上传或下载文件工具类
    ubuntu命令安装
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10591523.html
Copyright © 2011-2022 走看看