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     
  • 相关阅读:
    手写数字识别-卷积神经网络cnn(06-2)
    putty中文显示乱码解决方法
    linux可执行程序调试(c++)
    修改优化器进一步提升准确率(04-3)
    react 数组删除某一项更新setState无效的问题,react js怎么删除数组某一项,splice删除了某一项页面数据却不变
    高德地图实现一个比例圆环形聚合点缩放
    使用react context的作用React.createContext
    怎么在websotrm配置快捷启动vue-cli项目?
    vue项目找不到.eslintrc.js文件解决---帮助小白解决 'xxx' is defined but never used
    react hook性能优化使用memo、useCallback、useMemo
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5874548.html
Copyright © 2011-2022 走看看