zoukankan      html  css  js  c++  java
  • 九度oj 题目1460:Oil Deposit

    题目描述:

    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.

    输入:

    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.

    输出:

    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.

    样例输入:
    1 1
    *
    3 5
    *@*@*
    **@**
    *@*@*
    1 8
    @@****@*
    5 5
    ****@
    *@@*@
    *@**@
    @@@*@
    @@**@
    0 0
    
    样例输出:
    0
    1
    2
    2

    此题用深度优先搜索来求解,找到每一个'@',将其能遍历到的'@'均改成'*'
    能搜索几次就有几块.
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <queue>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 char map[102][102];
     9 int n, m;
    10 int dir[][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
    11 
    12 void dfs(int x, int y) {
    13     map[x][y] = '*';
    14     for (int i = 0; i < 8; i++) {
    15         int xt = x + dir[i][0];
    16         int yt = y + dir[i][1];
    17         if (xt >= 0 && xt < m && yt >= 0 && yt < n) {
    18             if (map[xt][yt] == '@') {
    19                 dfs(xt, yt);
    20             }
    21         }
    22     }
    23     
    24 }
    25 int main(int argc, char const *argv[])
    26 {
    27     //freopen("input.txt", "r", stdin);
    28     //freopen("output.txt", "w", stdout);
    29     while (scanf("%d %d", &m, &n) != EOF && m != 0) {
    30         for (int i = 0; i < m; i++) {
    31             scanf("%s", map[i]);
    32         }
    33         int ans = 0;
    34         for (int i = 0; i < m; i++) {
    35             for (int j = 0; j < n; j++) {
    36                 if (map[i][j] == '@') {
    37                     ans++;
    38                     dfs(i, j);
    39                 }
    40             }
    41         }
    42         printf("%d
    ", ans);
    43     }
    44     return 0;
    45     
    46 }
    47 
    48     
  • 相关阅读:
    单行文本溢出、多文本溢出
    移动端 顶部、底部和内容之间的小技巧 用flex布局来解决
    图片移动端高清适配问题 image-set 属性和@medie标签
    学习HTML5一周的收获2
    redis实操-sentinel
    spring 循环依赖的一次 理解
    (一)python 格式化 excel 格式
    (一) BIO,NIO, 阻塞,非阻塞,你懂了吗
    设计模式八 适配器模式
    2018年下半年小目标
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5874548.html
Copyright © 2011-2022 走看看