zoukankan      html  css  js  c++  java
  • Oil Deposits UVA

    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 le m le 100$ and $1 le n le 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
    题目很简单,求连通块,只是自己开始没用vis打标记错了,贴出来长点记性,以后最好还是用vis数组打标记把、、、
    这份代码运行错误,测试样例当然能过
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define maxn 105
    using namespace std;
    char mapn[maxn][maxn];
    int num,n,m;
    void dfs(int a,int b)
    {
        if(a>=n || a<0 || b>=m || b<0)
            return;
        for(int i=-1;i<=1;i++)
            for(int j=-1;j<=1;j++)
        {
            if(!i&&!j)
                continue;
            int xx = a + i;
            int yy = b + j;
            if(mapn[xx][yy] == '@')
            {
                mapn[xx][yy] = '*';
                dfs(xx,yy);
            }
        }
    }
    int main()
    {
        while(cin >> n >> m)
        {
            if(!n&&!m)
                break;
            num = 0;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    cin >> mapn[i][j];
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
            {
                if(mapn[i][j] == '@')
                {
                    num++;
                    mapn[i][j] = '*';
                    dfs(i,j);
                }
            }
            cout << num << endl;
        }
        return 0;
    }

    用vis标记的代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #define maxn 105
    using namespace std;
    char mapn[maxn][maxn];
    int num,n,m,vis[maxn][maxn];
    void dfs(int a,int b)
    {
        if(a>=n || a<0 || b>=m || b<0)
            return;
        if(vis[a][b] || mapn[a][b] != '@')
            return;
        vis[a][b] = 1;
        for(int i=-1;i<=1;i++)
            for(int j=-1;j<=1;j++)
        {
            if(!i&&!j)
                continue;
            int xx = a + i;
            int yy = b + j;
            if(mapn[xx][yy] == '@')
            {
                dfs(xx,yy);
            }
        }
    }
    int main()
    {
        while(scanf("%d%d",&n,&m))
        {
            if(!n&&!m)
                break;
            num = 0;
            memset(mapn,0,sizeof(mapn));
            memset(vis,0,sizeof(vis));
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    scanf("
    %c",&mapn[i][j]);
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
            {
                if(mapn[i][j] == '@' && !vis[i][j])
                {
                    num++;
                    dfs(i,j);
                }
            }
            printf("%d
    ",num);
        }
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    [Codeup 25482]选美
    [Codeup 25481] swan
    暑假集训D12总结
    [技术]浅谈重载操作符
    2020年寒假第6次学*进度记录
    2020年寒假第5次学*进度记录
    2020年寒假第4次学*进度记录
    “家庭记账本”软件开发(1)
    阅读《梦断代码》随笔(1)
    2020年寒假第三次学*进度记录
  • 原文地址:https://www.cnblogs.com/l609929321/p/6842482.html
Copyright © 2011-2022 走看看