zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    调试技巧--Windows端口号是否被占用
    如何制定自己的职业规划
    SQL总结(四)编辑类
    SQL总结(三)其他查询
    CompareAndSwap原子操作原理
    JVM调优之服务内存超过阈值报警
    Javassist中文技术文档
    微言Netty:分布式服务框架
    共享变量边界处理
    Netty客户端发送消息并同步获取结果
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/11069535.html
Copyright © 2011-2022 走看看