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     
  • 相关阅读:
    数据结构----------堆栈
    数据结构----------双向链表
    剑指offer32----之字形打印一颗二叉树
    剑指offer31----栈的压入、弹出序列
    MySQL----日期处理函数
    1.1 文档PUT内部原理
    1 集群状态、增删改查、全量替换、强制创建、设置单个index的分片数副本数
    编译问题:'<invalid-global-code>' does not contain a definition for 'Store' and no extension method 'XXX' accepting a first argument of type '<invalid-global-code>' could be found
    ES timeout 的一些笔记
    elasticsearch 5.0以上不支持consistency 和 quorum
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5874548.html
Copyright © 2011-2022 走看看