• hdu 1241 Oil Deposits


    Oil Deposits

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


    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
     
    PS:第一次提交就AC了,算是小欣慰吧
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cmath>
     4 #include<cstring>
     5 #include<cstdlib>
     6 using namespace std;
     7 int vis[103][103];
     8 char map[103][103];
     9 int n,m;
    10 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
    11             //方向左上,上,右上,左,右,左下,下,右下
    12 
    13 
    14 void dfs(int x,int y)
    15 {
    16     int ss,vv,i;
    17     for(i=0;i<8;i++)
    18     {
    19         ss=x+dir[i][0];
    20         vv=y+dir[i][1];
    21         if(ss>=0&&ss<n && vv>=0&&vv<m &&map[ss][vv]=='@' && !vis[ss][vv])
    22         {
    23             vis[ss][vv]=1;
    24             dfs(ss,vv);
    25         }
    26     }
    27     return ;
    28 }
    29 
    30 int main()
    31 {
    32     //freopen("in.txt","r",stdin);
    33     while(~scanf("%d%d",&n,&m))
    34     {
    35         memset(vis,0,sizeof(vis));
    36         int i,j;
    37         int sum=0;
    38         if(!n&&!m)
    39         break;
    40         for(i=0;i<n;i++)
    41         scanf("%s",map[i]);
    42         for(i=0;i<n;i++)
    43         {
    44             for(j=0;j<m;j++)
    45             {
    46                 if(!vis[i][j] && map[i][j]!='*')
    47                 {
    48                     dfs(i,j);
    49                     sum++;
    50                 }
    51             }
    52         }
    53         printf("%d
    ",sum);
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    bzoj 2742(树状数组)
    [网络流24题(3/24)] 最长k可重区间集问题(洛谷P3358)
    bzoj 1087(状压dp)
    算法模板整理V1.0
    ACM资料汇总
    算法笔记
    NC20861 兔子的逆序对(数学基础)
    zzuli新生周赛第四周题解
    Gym 102028E Resistors in Parallel(大数)
    HDU 3974 Assign the task(dfs序建线段树)
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4125968.html
Copyright © 2020-2023  润新知