• uva572 Oil Deposits


      Oil Deposits 

    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 le m le 100$ and $1 le n le 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
    

     题目大意: 通过bfs找出一个图中联通的块  

    n*m 图中 点从上到下编号 0,1,2,3...  第i 行j 列 (0开始) 编号 为 i*m+j;

     1 #include <cstdio>
     2 #include <iostream>
     3 
     4 using namespace std;
     5 
     6 const int maxn = 110;
     7 int n,m;
     8 char G[maxn][maxn];
     9 
    10 int q[maxn*maxn];
    11 int rear,fr;
    12 int visit[maxn][maxn];
    13 
    14 inline bool edge(int x,int y){
    15 
    16     if(x<0 || x>=n || y <0 || y>=m )
    17         return false;
    18     else
    19         return true;
    20 }
    21 int bfs(int i,int j)
    22 {
    23     if(G[i][j]=='@' && visit[i][j]==0)
    24     {
    25         if(edge(i+1,j)&&!visit[i+1][j]&&G[i+1][j]=='@')
    26             q[rear++]=(i+1)*m+j;
    27         if(edge(i+1,j-1)&&!visit[i+1][j-1]&&G[i+1][j-1]=='@')
    28             q[rear++]=(i+1)*m+j-1;
    29         if(edge(i+1,j+1)&&!visit[i+1][j+1]&&G[i+1][j+1]=='@')
    30             q[rear++]=(i+1)*m+j+1;
    31         if(edge(i,j-1)&&!visit[i][j-1]&&G[i][j-1]=='@')
    32             q[rear++]=(i)*m+j-1;
    33         if(edge(i,j+1)&&!visit[i][j+1]&&G[i][j+1]=='@')
    34             q[rear++]=(i)*m+j+1;
    35         if(edge(i-1,j-1)&&!visit[i-1][j-1]&&G[i-1][j-1]=='@')
    36             q[rear++]= (i-1)*m+j-1;
    37         if(edge(i-1,j+1)&&!visit[i-1][j+1]&&G[i-1][j+1]=='@')
    38             q[rear++]=(i-1)*m+j+1;
    39         if(edge(i-1,j)&&!visit[i-1][j]&&G[i-1][j]=='@')
    40             q[rear++]=(i-1)*m+j;
    41 
    42         visit[i][j]=1;
    43 
    44         int x,y;
    45         if(fr>rear-1)
    46             return 0;
    47 
    48     }
    49     return 0;
    50 }
    51 
    52 int main()
    53 {
    54     freopen("in.txt","r",stdin);
    55     cin>>n>>m;
    56     int ans=0;
    57     while(n&&m)
    58     {
    59         for(int i=0;i<n;i++)
    60             for(int j=0;j<m;j++)
    61                 cin>>G[i][j];
    62 
    63         for(int i=0;i<n;i++)
    64             for(int j=0;j<m;j++)
    65             {
    66                 if(!visit[i][j])
    67                 {
    68                     if(G[i][j]=='@')
    69                     {
    70                         bfs(i,j);
    71                         ans++;
    72                     }
    73                     else
    74                         visit[i][j]=1;
    75 
    76                 }
    77 
    78             while(fr<=rear-1)
    79             {
    80                 int x,y;
    81                 x=q[fr]/m;
    82                 y=q[fr]%m;
    83                 fr++;
    84                 bfs(x,y);
    85             }
    86         }
    87         cout<<ans<<endl;
    88         cin>>n>>m;
    89         for(int i=0;i<n;i++)
    90             for(int j=0;j<m;j++)
    91             {
    92                  G[i][j]=' ';
    93                 visit[i][j]=0;
    94             }
    95             ans=0;
    96     }
    97     return 0;
    98 }
  • 相关阅读:
    makefile基础(GNU)
    7z命令行工具
    使用getopt函数对windows命令行程序进行参数解析
    在iMac机os x上装win7双系统经验心得
    windows操作技巧
    【SpringBoot】SpringBoot 整合RabbitMQ(二十)
    【RabbitMQ】 RabbitMQ 基本概念及测试
    【SpringBoot】SpringBoot 整合Redis缓存(十九)
    【SpringBoot】SpringBoot 自定义starter(十七)
    【SpringBoot】SpringBoot 事件监听机制(十六)
  • 原文地址:https://www.cnblogs.com/doubleshik/p/3371336.html
Copyright © 2020-2023  润新知