zoukankan      html  css  js  c++  java
  • ZOJ-1709

    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

    典型的BFS问题;

    AC代码为:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<string>
     5 #include<queue>
     6 #include<algorithm>
     7 using namespace std;
     8 int vis[110][110];
     9 int m, n;
    10 char str[110][110];
    11 int bfx[8] = { 1,-1,0,0,1,1,-1,-1 };
    12 int bfy[8] = { 0,0,1,-1,1,-1,1,-1 };
    13 queue<int> q;
    14 void  BFS(int i, int j)
    15 {
    16 while (!q.empty())
    17 q.pop();
    18 q.push(i*n + j);
    19 
    20 
    21 while (!q.empty())
    22 {
    23 int u = q.front();
    24 q.pop();
    25 int cx = u / n;
    26 int cy = u % n;
    27 
    28 
    29 for (int k = 0; k<8; k++)
    30 {
    31 int nx = cx + bfx[k];
    32 int ny = cy + bfy[k];
    33 
    34 
    35 if (nx >= 0 && nx<m && ny >= 0 && ny<n && !vis[nx][ny] && str[nx][ny] == '@')
    36 {
    37 vis[nx][ny] = 1;
    38 q.push(nx*n + ny);
    39 }
    40 }
    41 }
    42 }
    43 
    44 
    45 int main()
    46 {
    47 
    48 
    49 while (~scanf("%d%d", &m, &n), m || n)
    50 {
    51 
    52 
    53 memset(vis, 0, sizeof(vis));
    54 int sum = 0;
    55 for (int i = 0; i<m; i++)
    56 {
    57 scanf("%s", str[i]);
    58 }
    59 for (int i = 0; i<m; i++)
    60 {
    61 for (int j = 0; j<n; j++)
    62 {
    63 if (str[i][j] == '@' && !vis[i][j])
    64 {
    65 vis[i][j] = 1;
    66 BFS(i, j);
    67 sum++;
    68 }
    69 }
    70 }
    71 
    72 
    73 printf("%d
    ", sum);
    74 }
    75 
    76 
    77 }
    View Code
  • 相关阅读:
    数字系统设计练习(一)—— 闪烁的小灯
    使用matplotlib时pyplot.show()图像不显示
    Linux命令——github篇
    Caffe: Cannot create Cublas handle. Cublas won't be available
    欧拉函数
    Prim最小生成树+优先队列优化
    KMP算法
    克鲁斯卡尔求最小生成树
    C++ 文件输出、输出
    并查集
  • 原文地址:https://www.cnblogs.com/csushl/p/9386591.html
Copyright © 2011-2022 走看看