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 }
  • 相关阅读:
    mysql 三星索引设置
    mysql 索引长度解释及不使用索引的一种特殊情况
    null作为方法的参数,并在方法里面赋值后的结果是什么?
    线程、调度线程池、异常
    系统服务化,需要考虑的问题
    05-Python之高级语法
    01-python基本语法元素
    04-Python之文件、异常和模块
    03-Python之类
    02-Python之函数
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/11069535.html
Copyright © 2011-2022 走看看