zoukankan      html  css  js  c++  java
  • HDU 1241 Oil Deposits dfs && bfs

    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
     
       下面有代码自己看。
       dfs
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,m,t,p;
    char map[105][105];
    int yi[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
    void dfs(int x,int y)
    {
        int i,j,rx,ry;
        map[x][y]='*';
        for (i=0;i<8;i++)
        {
             rx=x+yi[i][0];
             ry=y+yi[i][1];
            if (rx>=1 && rx<=n && ry>=1 && ry<=m && map[rx][ry]=='@')
            {
                map[rx][ry]='*';
                dfs(rx,ry);
            }
        }
        return ;
    }
    int main()
    {
        int i,j,ans;
        while (~scanf("%d%d",&n,&m))
        {
            if (n==0&&m==0) break;
            ans=0;
            getchar();
            for (i=1;i<=n;i++)
            for (j=1;j<=m;j++) scanf(" %c",&map[i][j]);
            for (i=1;i<=n;i++)
            for (j=1;j<=m;j++)
            if (map[i][j]=='@')
            {
                dfs(i,j);
                ans++;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

      bfs

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    int n,m;
    char map[105][105];
    int yi[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
    struct f
    {
        int x,y;
    };f s,r;
    void bfs(int x,int y)
    {
        int i,j;
        queue<f>q;
        s.x=x;
        s.y=y;
        map[x][y]='#';
        q.push(s);
        while (!q.empty())
        {
            s=q.front();
            q.pop();
            for (i=0;i<8;i++)
            {
                r.x=s.x+yi[i][0];
                r.y=s.y+yi[i][1];
                if (r.x>=0 && r.x<n && r.y>=0 && r.y<m && map[r.x][r.y]=='@')
                {
                    map[r.x][r.y]='#';
                    q.push(r);
                }
            }
        }
    }
    int main()
    {
        int i,j,ans;;
        while (~scanf("%d%d",&n,&m))
        {
            if (n==0&&m==0) break;
            ans=0;
            for (i=0;i<n;i++)
            for (j=0;j<m;j++) scanf(" %c",&map[i][j]);
            for (i=0;i<n;i++)
            for (j=0;j<m;j++)
            {
                if (map[i][j]=='@')
                {
                    ans++;
                    bfs(i,j);
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Python第二弹--------类和对象
    Python第一弹--------初步了解Python
    Java标记接口
    CentOS7下的YUM源服务器搭建详解,过程写的很详细(转)
    CentOS7.0安装Nginx 1.10.0
    QT中C++与Html端通信例子
    QT基础:QMainWindow学习小结
    QT基础:QT 定时器学习
    QT3D场景快速绘制入门学习
    QT编译错误:cannot find file: *.pro
  • 原文地址:https://www.cnblogs.com/pblr/p/4696385.html
Copyright © 2011-2022 走看看