• G


    G - Oil Deposits
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    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. 
     

    Input

    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. 
     

    Output

    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. 
     

    Sample Input

    1 1
    *
    3 5
    *@*@*
    **@**
    *@*@*
    1 8
    @@****@*
    5 5
    ****@
    *@@*@
    *@**@
    @@@*@
    @@**@
    0 0
     

    Sample Output

    0 1 2 2
     

    题意:输入两个数字分别代表输入字符的行和列,然后输入一组字符,里面只有@和*,让你求所有的@构成的块,只要@与@之间紧邻的有同行同列同对角线的关系中的一个就是同一块,让你求出所有这种块的个数;当输入0 0时就结束。

    分析:题目实质就是求连通块的块树。

    思路:每次从有石油的结点出发,用深度优先遍历思想(DFS),标志结点。每次进行遍历都能标记一个区域,区域的个数就是所求,具体可参考:数据结构基础之图部分的黑白图像部分

    AC代码:
      

    #include <cstdio>
    #include<cstring>
    const int maxz=100+5;
    char ch[maxz][maxz];
    int m,n,x[maxz][maxz];//该数组用来记路每一个点,防止重复
    void dfs(int r,int c,int id)
    {
    if(r<0||r>=m||c<0||c>=n) return;//"出界"的格子
    if(x[r][c]>0||ch[r][c]!='@') return;//不是“@”或者已经访问过的格子
    x[r][c]=id;//编号防止同一格子被多次访问
    for(int dr=-1;dr<=1;dr++)
    for(int dc=-1;dc<=1;dc++)//搜索周围的八个方向
    if(dr!=0||dc!=0) dfs(r+dr,c+dc,id);
    }
    int main()
    {
    while(scanf("%d%d",&m,&n)==2&&m&&n)
    {
    for(int i=0;i<m;i++)
    scanf("%s",ch[i]);//%s输入字符串
    memset(x,0,sizeof(x));//清零
    int count=0;
    for(int i=0;i<m;i++)
    for(int j=0;j<n;j++)
    if(x[i][j]==0&&ch[i][j]=='@') dfs(i,j,++count);//满足条件就加一,然后继续遍历
    printf("%d ",count);
    }
    return 0;
    }

    心得:通过解决这个题,加深了我对深度优先遍历的认知,学到了一些新的知识点,比如说我们可以用memset函数为数组清零,它的头文件为string.h,检索时我们可以定义两个for循环检索相邻的所有位置。其次,我总结了一下,不管是DFS还是BFS我们一般需要用两个数组,一个防止重复,一个用来记录输入的数据。(这只是我自己的一些见解,希望没有误导你)
     
     
     
  • 相关阅读:
    Java AbstractQueuedSynchronizer(AQS)
    CentOS Install Rancher & K3s
    CentOS Install Rancher & Kubernetes
    CentOS Install Kubernetes & Kubesphere
    支付宝小程序入门
    uni-app 抽奖
    APP上架
    uniapp微信小程序语音识别实现
    uniapp 安卓打包之导入的项目打包指导
    uniapp 支付宝 支付后不成功回调
  • 原文地址:https://www.cnblogs.com/lbyj/p/5675254.html
Copyright © 2020-2023  润新知