• 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;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    算法--字符串分隔符/字符串匹配算法
    VMware虚拟机磁盘收缩的几种方法
    maven学习--生命周期
    web适配问题
    eclipse按照svn插件
    一台服务器跑两个tomcat
    postgresql数据库
    visio2013破解工具
    多线程学习
    深入理解Java:SimpleDateFormat安全的时间格式化
  • 原文地址:https://www.cnblogs.com/l609929321/p/6596339.html
Copyright © 2020-2023  润新知