• hdu 1241 Oil Deposits (dfs)


    Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7813    Accepted Submission(s): 4583

    Problem 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
     
    Source
     
    Recommend

    Eddy

    题意:一块油田。@代表有油,*代表没油。数有多少块有油的地(水平方向、竖直方向或斜线方向有@在一起的只能当做整体算一次)。

    分析:直接两重循环,碰到@,计数器+1并且dfs将该位置周围相邻的@全部改为*,避免计数时重复。

     

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    char maps[105][105];
    int dx[]={-1,1,0,0,-1,1,-1,1};
    int dy[]={0,0,-1,1,-1,1,1,-1};
    int r,c,flag;
    
    void dfs(int x,int y)
    {
        maps[x][y]='*';
        int xx,yy;
        for(int i=0;i<8;i++)
        {
            xx=x+dx[i];
            yy=y+dy[i];
    
            if(xx>=1&&xx<=r&&yy>=1&&yy<=c&&maps[xx][yy]=='@')
            {
    			//printf("test: %d %d
    ",xx,yy);
                maps[xx][yy]='*';
                dfs(xx,yy);
            }
        }
    }
    
    int main()
    {
        int i,j,res;
    	char ch[30];
        while(scanf("%d%d",&r,&c)&&r&&c)
        {
    		//getchar();
    		//getchar();
    		gets(ch);      //也可以用两次getchar()
            res=0;
            for(i=1;i<=r;i++)
    			gets(maps[i]+1);
            for(i=1;i<=r;i++)
    			for(j=1;j<=c;j++)
                {
                    if(maps[i][j]=='@')
                    {
                        ++res;
                        dfs(i,j);
                    }
                }
            printf("%d
    ",res);
        }
        return 0;
    }
    


     

    8727690

    2013-07-27 00:11:34

    Accepted

    1241

    0MS

    308K

    979 B

    C++

     

     

  • 相关阅读:
    AC 自动机
    [HihoCoder-1424] Asa's Chess Problem
    C++ Tricks
    2017"百度之星"程序设计大赛
    后缀自动机
    ASP教程:gb2312和utf-8乱码问题解决
    cryto-js 常用加密库 md5加密
    nrm是什么?以及nrm的安装与命令
    MongoDB 安装
    koa2 安装与启动
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3220316.html
Copyright © 2020-2023  润新知