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;
    }
    
  • 相关阅读:
    AppCrawler自动化遍历使用详解(版本2.1.0 )
    python 接口测试1 --如何创建和打印日志文件
    通过Xshell登录远程服务器实时查看log日志
    java基础知识6-- 抽象类,抽象方法,接口,构造方法,类方法等易混淆的知识点
    java基础知识5--集合类(Set,List,Map)和迭代器Iterator的使用
    java基础知识4--数组的常用方法(Array)
    java基础知识3--如何获取资源文件(Java中获取资源文件的url)
    java基础知识2--String,StringBufffer,StringBuilder的区别
    java基础知识1--String常用方法总结
    javascript正则表达式验证密码(必须含数字字符特殊符号,长度4-16位之间)
  • 原文地址:https://www.cnblogs.com/pblr/p/4696385.html
Copyright © 2011-2022 走看看