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;
    }
    
  • 相关阅读:
    ZooKeeper基本原理
    ElasticSearch的基本原理与用法
    Solr与MySQL查询性能对比
    MySQL性能优化总结
    Java并发集合及线程池实现原理
    Java垃圾回收机制
    Java Spring的IoC和AOP的知识点速记
    基于Solr的空间搜索
    系统学习消息队列分享(十) 如何实现高性能的异步网络传输?
    系统学习消息队列分享(九) 如何使用异步设计提升系统性能?
  • 原文地址:https://www.cnblogs.com/pblr/p/4696385.html
Copyright © 2011-2022 走看看