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;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例
    【Java基础】1、java中的instanceof
    【Java面试】1、基础知识篇
    【github&&git】3、git图像化界面GUI的使用
    【github&&git】2、github入门到上传本地项目
    Spring Boot属性配置文件详解
    微服务Spring Cloud与Kubernetes比较
    导入时如何定制spring-boot依赖项的版本
    spring容器ApplicationContext初始化(spring应用上下文初始化)
    spring概念简介、bean扫描与注册实现方式
  • 原文地址:https://www.cnblogs.com/l609929321/p/6842482.html
Copyright © 2011-2022 走看看