zoukankan      html  css  js  c++  java
  • ZOJ 1709


    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 <= 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


    Source: Mid-Central USA 1997

    Submit    Status

    依旧是BFS

    练习熟练度

     1 #include <iostream>
     2 #include <cstring>
     3 #include <queue>
     4 #include <string>
     5 using namespace std;
     6 const int maxm = 105;
     7 int m,n,num;
     8 char grid[maxm][maxm];
     9 int visit[maxm][maxm];
    10 int step[8][2]={{-1,1},{-1,0},{-1,-1},{0,1},{0,-1},{1,1},{1,0},{1,-1}};
    11 class Node
    12 {
    13 public:
    14     int x,y;
    15 
    16     void init(int a,int b)
    17     {
    18         x = a;
    19         y = b;
    20     }
    21 };
    22 
    23 void bfs(Node begin)
    24 {
    25     num++;
    26     queue<Node> Q;
    27     Q.push(begin);
    28     visit[begin.x][begin.y]=num;
    29     while(!Q.empty())
    30     {
    31         Node a = Q.front();
    32         Q.pop();
    33         for(int i=0;i<8;i++)
    34         {
    35             int x = a.x+step[i][0];
    36             int y = a.y+step[i][1];
    37             if(x>=0&&x<m&&y>=0&&y<n&&visit[x][y]==0&&grid[x][y]=='@')
    38             {
    39                 Node b;
    40                 b.init(x,y);
    41                 Q.push(b);
    42                 visit[x][y]=num;
    43             }
    44         }
    45     }
    46 }
    47 void printVisit()
    48 {
    49     for(int i =0;i<m;i++)
    50     {
    51         for(int j=0;j<n;j++)
    52         {
    53             cout<<visit[i][j]<<" ";
    54         }
    55         cout<<endl;
    56     }
    57 }
    58 int main()
    59 {
    60     while(cin>>m>>n)
    61     {
    62         if(m+n==0)
    63             return 0;
    64         for(int i=0;i<m;i++)
    65         {
    66             string s;
    67             cin>>s;
    68             for(int j=0;j<n;j++)
    69             {
    70                 grid[i][j]=s.at(j);
    71             }
    72         }
    73         memset(visit,0,sizeof(visit));
    74         num=0;
    75         for(int i=0;i<m;i++)
    76         {
    77             for(int j=0;j<n;j++)
    78             {
    79                 if(visit[i][j]==0&&grid[i][j]=='@')
    80                 {
    81                     Node begin;
    82                     begin.init(i,j);
    83                     bfs(begin);
    84                 }
    85             }
    86         }
    87 
    88         cout<<num<<endl;
    89 
    90     }
    91     return 0;
    92 }

     

  • 相关阅读:
    SQL中关于Left Join转为Inner Join的问题,即左关联转为内关联的问题
    Mybatis Plus 2 升到 Mybatis Plus 3 时,oracle 自增序列的相关问题
    Java项目启动时,oracle 驱动异常
    window 下安装 Arthas
    postman 中给所有接口token授权的配置
    探讨:在循环前与在循环中创建对象的区别
    当你无法发现问题所在时,不要简单地把代码或者数据还原
    http://875880923.iteye.com/blog/1963400
    2013成都网络赛 J A Bit Fun(水题)
    2013成都网络赛 C We Love MOE Girls(水题)
  • 原文地址:https://www.cnblogs.com/Run-dream/p/3880380.html
Copyright © 2011-2022 走看看