• HDU 1241 Oil Deposits


    Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 29268    Accepted Submission(s): 16888


    Problem 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
    Source
    是求连通块的个数,意思是求几组数据每组数据连通块的个数,@代表油田
    用dfs
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<map>
    #include<algorithm>
    using namespace std;
    char c[105][105];
    int visit[105][105];
    int num,n,m;
    void dfs(int a,int b,int id)
    {
        if(a>=n||b>=m||a<0||b<0)
            return;
        if(visit[a][b]>0||c[a][b]!='@')
           return;
        visit[a][b]=id;
        for(int p=-1;p<=1;p++)
           for(int q=-1;q<=1;q++)
           {
                 if(q!=0||p!=0)
                    dfs(p+a,q+b,id);//往上下左右搜索判断
           }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            getchar();
            if(n==0&&m==0)
                break;
            num = 0;
            memset(c,0,sizeof(c));
            memset(visit,0,sizeof(visit));
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    cin >> c[i][j];
            /*for(int i=0;i<n;i++)
                puts(c[i]);*/
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
            {
                if(c[i][j]=='@'&&visit[i][j]==0)
                    dfs(i,j,++num);
            }
            printf("%d
    ",num);
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    自己写的一些公共js方法
    node(03)--利用 HTTP 模块 URl 模块 PATH 模块 FS 模块创建一个 WEB 服务器
    node(03 fs文件模块)
    node (02 CommonJs 和 Nodejs 中自定义模块)顺便讲讲module.exports和exports的区别 dependencies 与 devDependencies 之间的区别
    node.js (01http 模块 url 模块)
    Ubuntu 16.04 下python2和python3及对应的pip的方法
    MySQL划重点-查询-聚合
    MySQL划重点-查询-条件
    MySQL命令
    使用Navicat for Mysql连接装在虚拟机Ubuntu16.04上的mysql服务器
  • 原文地址:https://www.cnblogs.com/l609929321/p/6596339.html
Copyright © 2020-2023  润新知