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
  • 相关阅读:
    读书笔记,《我还是喜欢东京——带你感受城市细节》
    学习笔记:Maven的ArcheType的学习笔记
    如何从中企动力(新网)转移域名到阿里云(万网)
    Maven自定义Archetype(zz)
    读书笔记,《Java 8实战》第五章,使用流
    读书笔记,《Java 8实战》,第四章,引入流
    读书笔记,《Java 8实战》,第三章,Lambda表达式
    读书笔记,《Java8实战》第一章,为什么要关心 Java8
    读书笔记,《深入理解java虚拟机》,第三章 垃圾收集器与内存分配策略
    行业知识:关于发电量与碳排放和等效植树的换算关系
  • 原文地址:https://www.cnblogs.com/csushl/p/9386591.html
Copyright © 2011-2022 走看看