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 }
  • 相关阅读:
    一、Javadoc文档标记
    0-写在java系列文章之前
    Tomcat全攻略
    linux使用普通账户时,无法登录,提示“-bash: fork: retry: Resource temporarily unavailable”
    在Linux下安装和使用MySQL
    linux下修改jdk环境变量的方法
    linux下卸载系统自带或者非自带的jdk
    linux中 /etc/profile的作用
    每天一个linux命令:tar命令-jia2
    如何使用蓝湖设计稿同时适配PC及移动端
  • 原文地址:https://www.cnblogs.com/zuiaimiusi/p/11069535.html
Copyright © 2011-2022 走看看