zoukankan      html  css  js  c++  java
  • HDU-1241

    先直接上题,这题比POJ的单身题(1111)要稍微简单一点

    Oil Deposits

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 31004    Accepted Submission(s): 18009


    Problem Description
    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
     
    题意:给你一个矩阵含有@和*,想法跟POJ1111差不多(有疑问请看POJ1111的题解),但最终求的是最终有多少个被感染的区域。
     
    思路:按行依次读,碰到@时将@该为*,并对该位置进行DFS,每一步遍历得到的@都该为*。最后统计按行读的时候碰到几个位置需要深搜,最后得出被感染的区域
     
    AC代码如下:
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 char M[105][105];
     5 int dir[8][2] = { {1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1} };
     6 int DFS(int h, int l)
     7 {
     8     if (M[h][l] == '*') return 0;
     9     M[h][l] = '*';
    10     for (int i = 0;i < 8;i++)
    11     {
    12         DFS(h + dir[i][0], l + dir[i][1]);
    13     }
    14     return 0;
    15 }
    16 int main()
    17 {
    18     int m, n;
    19     while (cin >> m >> n)
    20     {
    21         if (m == 0 && n == 0) break;
    22         int sum = 0;
    23         memset(M, '*', sizeof(M));
    24         for (int i = 1;i <= m;i++)
    25             for (int j = 1;j <= n;j++)
    26                 cin >> M[i][j];
    27         for(int i=0;i<=m;i++)
    28             for (int j = 0;j <= n;j++)
    29             {
    30                 if (M[i][j] == '@')
    31                 {
    32                     DFS(i, j);
    33                     sum++;
    34                 }
    35             }
    36         cout << sum << endl;
    37     }
    38     return 0;
    39 }

    这道题主要想法就是将碰到的@变为*,在深搜的过程中进行改变。

    终于完成第二篇随笔了,突然发现写博客还是蛮有意思的,希望各路大大能为我多指导指导,感谢!

     
  • 相关阅读:
    js_sl 分享
    js_sl 延迟菜单
    jszs 历史管理
    jszs 对象引用
    jszs 快速排序
    jszs 枚举算法
    dom cookie记录用户名
    dom 拖拽回放
    dom div重合提示
    dom 输入文字模拟滚动
  • 原文地址:https://www.cnblogs.com/forever-snow/p/7098922.html
Copyright © 2011-2022 走看看