• Uva572


    https://vjudge.net/problem/UVA-572

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides 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 containsone 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

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 const int maxn=105;
     5 using namespace std;
     6 char a[maxn][maxn];
     7 int n,m;
     8 int vis[maxn][maxn];
     9 int xx[8]={1,1,0,-1,-1,-1,0,1};
    10 int yy[8]={0,1,1,1,0,-1,-1,-1};
    11 void dfs(int x,int y)
    12 {
    13     if(x<0||x>=m||y<0||y>=n)return;
    14     if(vis[x][y]||a[x][y]!='@')return;
    15     vis[x][y]=1;
    16     for(int i=0;i<8;i++)
    17         for(int j=0;j<8;j++)
    18         dfs(x+xx[i],y+yy[i]);
    19 }
    20 int main()
    21 {
    22     while(scanf("%d %d",&m,&n)==2,m,n)
    23     {
    24         for(int i=0;i<m;i++)
    25             scanf("%s",a[i]);
    26         int cnt=0;
    27         memset(vis,0,sizeof vis);
    28         for(int i=0;i<m;i++)
    29             for(int j=0;j<n;j++)
    30             if(a[i][j]=='@'&&!vis[i][j])
    31                 dfs(i,j),cnt++;
    32         printf("%d
    ",cnt);
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    用EnumDisplaySettings获取显示设置信息
    关于同一线程中临界区编程的
    HGE初始化状态设置
    Delphi 在ListView中添加一个进度条
    WM_NCCALCSIZE消息处理详解
    Delphi操作系统菜单
    C语言I博客作业03
    C语言I博客作业05
    大一第一周作业
    C语言I博客作业02
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/11069535.html
Copyright © 2020-2023  润新知