[kuangbin]专题一 简单搜索 Oil Deposits HDU - 1241【DFS】

【题目描述】
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
GeoSurvComp地质调查公司负责地下石油矿床的探测。GeoSurvComp一次使用一个很大的矩形区域,并创建一个网格,将土地划分成许多正方形的地块。然后,它分别分析每个地块,使用传感设备来确定该地块是否含有石油。一个含有油的地块叫做口袋。如果两个小孔相邻,那么它们是同一油田的一部分。石油储量可能相当大,而且可能含有许多小孔。你的工作是确定在一个网格中包含了多少不同的石油储量。

【输入】
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*’, representing the absence of oil, or `@’, representing an oil pocket.
输入文件包含一个或多个网格。每个网格以包含m和n的一行开始,这是网格中的行数和列数,由单个空格分隔。如果m=0时,则表示输入结束;否则,1 <= m <= 100 and 1 <= n <= 100。后面是n个字符的m行(不包括行尾字符)。每个字符对应于一个情节,要么是‘*’,代表没有油,要么‘@’,代表一个油袋。

【输出】
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
对于每个网格,输出不同的油库数量。两个不同的口袋是同一油藏的一部分,如果它们是水平、垂直或对角线相邻的。一个石油矿床不会有超过100个口袋。

【样例输入】
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

【样例输出】
0
1
2
2

题目链接:https://cn.vjudge.net/problem/HDU-1241

dfs就完事了

代码如下:

#include <iostream>
#include <cstring>
using namespace std;
static const int MAXN=100;
static const int dx[]={-1,-1,-1,0,0,1,1,1},dy[]={-1,0,1,-1,1,-1,0,1};
char mp[MAXN+10][MAXN+10];
bool vis[MAXN+10][MAXN+10];
int m,n;
void dfs(int x,int y)
{
    for(int i=0;i<8;i++)
    {
        if(1<=x+dx[i] && x+dx[i]<=m && 1<=y+dy[i] && y+dy[i]<=n && mp[x+dx[i]][y+dy[i]]=='@' && !vis[x+dx[i]][y+dy[i]])
        {
            vis[x+dx[i]][y+dy[i]]=true;
            dfs(x+dx[i],y+dy[i]);
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    while(cin>>m>>n && m && n)
    {
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
                cin>>mp[i][j];
        memset(vis,false,sizeof(vis));
        int cnt=0;
        for(int i=1;i<=m;i++)
            for(int j=1;j<=n;j++)
                if(mp[i][j]=='@' && !vis[i][j])
                {
                    vis[i][j]=true;
                    dfs(i,j);
                    cnt++;
                }
        cout<<cnt<<endl;
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin带你飞」专题五并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值