zoukankan      html  css  js  c++  java
  • 杭电 1241 Oil Deposits

    Oil Deposits

    Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
    Total Submission(s) : 4 Accepted Submission(s) : 4
    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

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int i,j,n,m;
    int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
    char a[101][101];
    void dfs(int x,int y)
    {
        int x1,y1;
        for(int h=0;h<8;h++)
        {
            x1=x+dir[h][0];
            y1=y+dir[h][1];
            if(x1>=1&&x1<=m&&y1>=1&&y1<=n)//判断边界
            {
                if(a[x1][y1]=='@')//搜索条件 
                {
                    a[x1][y1]='*';
                    dfs(x1,y1);//递归搜索 
                }
            }
        }
    }
    int main()
    {
        while(cin>>m>>n)
        {
            int sum=0;
            if(m==0&&n==0)
                break;
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                    cin>>a[i][j];
            }
            for(i=1;i<=m;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(a[i][j]=='@')//搜索起点
                    {
                        sum++;
                        a[i][j]='*';
                        dfs(i,j);
                    }
                }
            }
            cout<<sum<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    EJB3.0
    EJB
    到底EJB是什么
    EJB到底是什么,真的那么神秘吗??
    ssh和ejb的区别
    struts2的一些小问题
    Struts2中ValueStack结构和总结
    Java 监听器
    浅谈cookie 和session 的区别
    理解Cookie和Session机制
  • 原文地址:https://www.cnblogs.com/nanfenggu/p/7900186.html
Copyright © 2011-2022 走看看