zoukankan      html  css  js  c++  java
  • 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;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    C#中datagridview单元格值改变实现模糊查找
    C#中建立treeView
    L2008 最长对称子串
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)
    GPLT团体程序设计天梯赛练习集 L1041~L1050
    GPLT团体程序设计天梯赛练习集 L1051~L1060
    GPLT团体程序设计天梯赛练习集 L1061~L1072
    第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(昆明)(热身赛)
    L2007 家庭房产 并查集
    利用反射把查询到的Table、Reader转换成List、Model
  • 原文地址:https://www.cnblogs.com/l609929321/p/6596339.html
Copyright © 2011-2022 走看看